Differences between revisions 6 and 7
Revision 6 as of 2008-05-07 22:11:10
Size: 2284
Editor: GreyCat
Comment: clean up
Revision 7 as of 2008-05-20 13:58:31
Size: 2221
Editor: GreyCat
Comment: clean up, adjust link
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
 {{{
 myprog & # Start program in the background
 daemonpid=$! # ...and save its process id
{{{
# Bourne
myprog & # Start program in the background
daemonpid=$! # ...and save its process id
Line 9: Line 10:
 while sleep 60
 do
     if kill -0 $daemonpid # Is the process still alive?
     then
         echo >&2 "OK - process is still running"
     else
         echo >&2 "ERROR - process $daemonpid is no longer running!"
         break
     fi
 done}}}
while sleep 60
do
    if kill -0 $daemonpid # Is the process still alive?
    then
        echo >&2 "OK - process is still running"
    else
        echo >&2 "ERROR - process $daemonpid is no longer running!"
        break
    fi
done}}}

'''NOTE''': Anything you do that relies on PIDs to identify a process is inherently flawed. If a process dies, the meaning of its PID is UNDEFINED. Another process started afterward may take the same PID as the dead process. That would make the previous example think that the process is still alive (its PID exists!) even though it is dead and gone. It is for this reason that nobody should try to manage processes other than the parent of that process. Read ProcessManagement.
Line 22: Line 25:
'''NOTE''': Anything you do that relies on PIDs to identify a process is inherently flawed. If a process dies, the meaning of its PID is UNDEFINED. Another process started afterward may take the same PID as the dead process. That would make the previous example think that the process is still alive (its PID exists!) even though it is dead and gone. It is for this reason that nobody should try to manage processes other than the parent of that process. Read the link at the end of this FAQ.

More often, there's some ulterior motive, such as the desire to ensure that some daemon which is known to crash frequently is still running. If this is the case, the best course of action is to ''fix the program or its configuration'' so that it stops dying, rather than merely restarting it each time it dies. If you can't do that, then use something like this:
More often, there's some ulterior motive, such as the desire to ensure that some daemon which is known to crash frequently is still running. If this is the case, the best course of action is to ''fix the program or its configuration'' so that it stops crashing. If you can't do that, then just restart it when it dies:
Line 27: Line 28:
until myprog # POSIX
while true
Line 29: Line 31:
    echo "ERROR: myprog terminated with exit code: $?. Restarting .."
    sleep 1
  myprog && break
  sleep 1
Line 34: Line 36:
This piece of code will restart `myprog` if it terminates with an exit code other than 0 (indicating something went wrong). If the exit code is 0 (successfully shut down) the loop ends. The latter case generally speaking happens only when you instruct the program to shut down, in which case you don't want it to restart itself automatically. This piece of code will restart `myprog` if it terminates with an exit code other than 0 (indicating something went wrong). If the exit code is 0 (successfully shut down) the loop ends. (If your process is crashing but also returning exit status 0, then adjust the code accordingly.) Note that `myprog` must run in the foreground. If it automatically "daemonizes" itself, you are screwed.
Line 36: Line 38:
For a much better discussion of these issues, see ProcessManagement or [:BashFAQ#faq33:FAQ #33]. For a much better discussion of these issues, see ProcessManagement or [:BashFAQ/033:FAQ #33].

Anchor(faq42)

How can I find out if a process is still running?

The kill command is used to send signals to a running process. As a convenience function, the signal "0", which does not exist, can be used to find out if a process is still running:

# Bourne
myprog &          # Start program in the background
daemonpid=$!      # ...and save its process id

while sleep 60
do
    if kill -0 $daemonpid       # Is the process still alive?
    then
        echo >&2 "OK - process is still running"
    else
        echo >&2 "ERROR - process $daemonpid is no longer running!"
        break
    fi
done

NOTE: Anything you do that relies on PIDs to identify a process is inherently flawed. If a process dies, the meaning of its PID is UNDEFINED. Another process started afterward may take the same PID as the dead process. That would make the previous example think that the process is still alive (its PID exists!) even though it is dead and gone. It is for this reason that nobody should try to manage processes other than the parent of that process. Read ProcessManagement.

This is one of those questions that usually masks a much deeper issue. It's rare that someone wants to know whether a process is still running simply to display a red or green light to an operator.

More often, there's some ulterior motive, such as the desire to ensure that some daemon which is known to crash frequently is still running. If this is the case, the best course of action is to fix the program or its configuration so that it stops crashing. If you can't do that, then just restart it when it dies:

# POSIX
while true
do
  myprog && break
  sleep 1
done

This piece of code will restart myprog if it terminates with an exit code other than 0 (indicating something went wrong). If the exit code is 0 (successfully shut down) the loop ends. (If your process is crashing but also returning exit status 0, then adjust the code accordingly.) Note that myprog must run in the foreground. If it automatically "daemonizes" itself, you are screwed.

For a much better discussion of these issues, see ProcessManagement or [:BashFAQ/033:FAQ #33].

BashFAQ/042 (last edited 2012-10-27 10:38:13 by a88-114-128-29)