How do i restart a process if it fails?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i restart a process if it fails?
# 1  
Old 03-27-2014
How do i restart a process if it fails?

Hi Guru's,

I just want to have an idea on how to restart a particular step when it fails?

SCENARIO
we have plenty of steps such as the following below:

Step 1
copy file from source to target location which is in a different server.

Step 2
create initial and incremental process

Step 3
Start initial extract from source location

and etc...

say step 3 fails, what i wanted to do is to just restart that step where it fails without restarting the whole process.

Any idea is much appreciated.
# 2  
Old 03-27-2014
I think you can check/verify the status of step 3 and if the return status of step 3 is not successful you can re-run it along with counter re-run variable.
# 3  
Old 03-27-2014
If you are crashing out of your code so you can fix the problem and you want to restart, you could pass in the step as an argument then each part of the main code can check if it is due to run or not. For example:-
Code:
$ cat step_restart_straight
#!/bin/ksh
# Just a quick step-restart demo

step="${1:-0}"

# Section 1
if [ $step -ge 1 ]
then
   echo "This is running Section 1."
fi

# Section 2
if [ $step -ge 2 ]
then
   echo "This is running Section 2."
fi

# Section 3
if [ $step -ge 3 ]
then
   echo "This is running Section 3."
fi

# Section 4
if [ $step -ge 4 ]
then
   echo "This is running Section 4."
fi
$ step_restart_straight
This is running Section 1.
This is running Section 2.
This is running Section 3.
This is running Section 4.
$
$ step_restart_straight 3
This is running Section 3.
This is running Section 4.
$


Another approach would be to use functions and have a loop round a case statement.
Code:
cat step_restart_functions
#!/bin/ksh
# Just a quick step-restart demo

function_one()
{
 echo "This is function 1"
}

function_two()
{
 echo "This is function $step."
}

function_three()
{
 echo "This is function $step."
}

function_four()
{
 echo "This is function 4"
}

step="${1:-0}"

while :
do
 case $step in
  0) : ;;                          # No action for zero step
  1) function_one ;;
  2) function_two ;;
  3) function_three ;;
  4) function_four ;;
  *) exit ;;
 esac

 ((step=$step +1))            # Increment count to go to next step
done                               # Go round and run next step
$ step_restart_functions  
This is function 1
This is function 2.
This is function 3.
This is function 4
$
$ step_restart_straight 3
This is function 3.
This is function 4
$

You can even use (as you can see) the value of the step within the function, so that may help /logging for messages or debug later.



I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 4  
Old 03-27-2014
# 5  
Old 03-28-2014
Thanks a lot. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Can't stop/restart postfix - pid associated with another process

This issue could happen to any other service but in this case its commssioning Postfix - it seems i can't stop postfix as the PID relates to another service - i've delete the 'master.lock' but to no available - any ideas, memeory commands etc ? thanks in advance ps. the serve is in Production so... (2 Replies)
Discussion started by: stevie_velvet
2 Replies

2. Shell Programming and Scripting

Monitor and restart UNIX process

Hi all, Tearing my hair out..! I have a requirement to monitor and restart a unix process via a simple watchdog script. I have the following 3 scripts that dont work for me.. script 1 (only produces 1 output if process is up or not)... (4 Replies)
Discussion started by: jonnyd
4 Replies

3. Shell Programming and Scripting

Script to restart process

HI, I am trying to write a scrip which would restart active process. This is what i have written till now. $ xms show pr PRESE.* NAME STATUS ROLE PID RSTRTS STARTED MACHINE... (8 Replies)
Discussion started by: Siddheshk
8 Replies

4. Emergency UNIX and Linux Support

Check hung process and restart

Hi all I have networker running on a RHEL 5.7 and over time it hangs. So the solution backup team proposed is to check if the process is hung, to stop and start it. Unfortunately for me, the rc script only allows three commands, start, stop and status (no restart option) so I managed to set... (15 Replies)
Discussion started by: hedkandi
15 Replies

5. Shell Programming and Scripting

Script to restart a process

I have written a script which checks for a file if that is being updated or not. If the files is not being updated then it will restart the process. #!/bin/sh DATE=`date +%Y%m%d%H%M%S` LOG_FILE=/var/xmp/log/XMP_* INCEPT=`ls -l $LOG_FILE |awk '{print $5}'` PROC=`xms show pr |grep -i... (3 Replies)
Discussion started by: Siddheshk
3 Replies

6. Shell Programming and Scripting

How to keep process running after apache restart.

I have posted this on the Web subforum but it seems that nobody knows to do this, maybe someone has a solution here. Thank you I have a PHP application that starts a couple of processes on the server...the problem is that if I restart apache those running apps will die. How can I start them... (1 Reply)
Discussion started by: valiadi
1 Replies

7. Web Development

How to keep process running after apache restart.

Hi, I have a PHP application that starts a couple of processes on the server...the problem is that if I restart apache those running apps will die. How can I start them in a way that they are not killed when I restart/stop apache ? $cmdstr = "nohup ".$config."/".$config."... (6 Replies)
Discussion started by: valiadi
6 Replies

8. Shell Programming and Scripting

restart process based on file

Hi all. I do have a script "startApp.sh" (app result is a file /opt/extract/appextract.txt) I have no problems with stopping app var1=`ps -ef | grep -v grep | grep MyApp | awk '{print $2}'` kill -9 $var1 What I want to achieve is: I start app, app is doing some extraction, after... (11 Replies)
Discussion started by: e-l-diablo
11 Replies

9. SuSE

Restart process

I have a process that gradually eats up memory, it's currently at 80.2% and slowing down the linux server > ps aux | grep SNMPME root 3129 0.0 80.2 3591752 2480700 ? Sl Feb13 5:04 /opt/nampe/lib/snmpme/SNMPME config/startup.xml Is there a command I can execute to restart this... (3 Replies)
Discussion started by: brendan76
3 Replies

10. Shell Programming and Scripting

suspend/restart a process in shell script

Hi, I have a task that Im stuck on. I have an elementary script named 'myscript' that prints "the script is running" once a second. It runs for 27 seconds. I need to write a 2nd script that starts 'myscript' and takes a parameter '$1' for a number. my 2nd script then needs to pause myscript... (1 Reply)
Discussion started by: daneensign
1 Replies
Login or Register to Ask a Question