View Single Post

  #4  
Old 06-29-2008, 11:03 PM
Default Re: How to get an "alert" when a process dies

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
> process 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.


Totally untested, but it should get your creative juices flowing:

#!/bin/bash
PROC=someprocess # The process to check
WAIT=10 # Seconds to wait between checks

while [ 1 ]
do
ps -ef|grep $PROC &>/dev/null
if [ $? -ne 0 ]; then
# grep didn't find "$PROC"...dead?
/etc/init.d/$PROC restart &>/dev/null

# Send some notification saying what happened.
# Maybe re-run the process check??
fi

# Now sleep and re-check the process after a specified delay
sleep $WAIT
done


Now all you need to do, is start that, fork it to the background and voila.
Add whatever notification method you want in the "if...fi" block. This
script will run until you (or something else) kills it by virtue of
the "while" loop.

If you really wanted to get fancy and check a few processes, change "PROC"
into an array and iterate through it, or use a "for PROC in proc1 proc2
etc" type construct. There are many ways to extend this, but whatever you
need to do can be based on this to provide sub-minute scheduling.

Cheers,

James
--
It's lucky you're going so slowly, because you're going in the wrong
direction.

Reply With Quote