
06-30-2008, 08:16 PM
|
Re: How to get an "alert" when a process dies In article <486902DF.3040302@gmail.com>,
Nico Kadel-Garcia wrote:
:Robert Nichols wrote:
:> In article ,
:> nicc777 wrote:
:> :Hi all,
:> :
:> :Even though I use Linux a lot, I have not been doing a lot of hard
:> :core Linux sysadmin stuff for some time, hence the question here :-)
:> :
:> :Is there a way to trigger an event (like running a script) when a
:> rocess dies? Perhaps even a script to restart a process if it detects
:> :that it's dead?
:> :
:> :I have been using cron for monitoring processes up to now, but the
:> :monitoring interval is 1 minute apart. I have a project (streaming of
:> :nature) that should not go down, but the problem is that sometimes it
:> :dies and it takes up to a minute before the traditional monitoring
:> :script will restart it.
:> :
:> :Any ideas?
:>
:> Can't you just run your process from a simple script that loops,
:> restarting your process whenever it terminates:
:>
:> while :
:> do
:> myprocess [args ... ]
:> done
:>
:> Alternatively, if this process is one that you want to start when the
:> system boots then you could run it directly from /etc/inittab, using
:> "respawn" in the action field. If you do that, you'll probably want
:> to invoke your process via 'su' or else it will be running with root
:> privileges.
:>
:
:Put a pause in there, of say a second, your script is going to hoover up
:whatever CPU is available for itself.
Curious. I've got a couple of scripts that basically look like that,
and the shell obediently waits, consuming no CPU whatever. It will use
CPU time only if 'myprocess' keeps terminating immediately. And, if
that process is as vital as the OP suggests, having a shell sitting in a
loop trying to restart it isn't a big problem, though flooding some log
file with error messages could indeed be nasty.
Anyway, my suggestion wasn't really intended as a complete solution.
For bulletproofing, you'd want to check whether the program you are
trying to invoke exists and is executable, and limit the restart
rate if it keeps dying immediately. Something like:
#!/bin/bash
PROG=/bin/true
N=0
while :
do
if [ ! -x $PROG ]; then
echo "$PROG is not executable" >&2
exit 1
fi
if [ $N -lt $SECONDS ]; then
N=$SECONDS
elif [ $(($N - 5)) -ge $SECONDS ]; then
echo "$PROG restarting too frequently -- sleeping" >&2
sleep 60
fi
N=$(($N+1))
$PROG [args ... ]
done
--
Bob Nichols AT comcast.net I am "RNichols42" |