Re: How to get an "alert" when a process dies On Jun 30, 5:03*am, Centurion wrote:
> 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.
Thanks a lot - I came to the same conclusion using another scripting
language, but I still sit with a 1 second delay - which I assume is
sufficient for my current scenario.
Still - I would like to know if there is a real time notification
mechanism.
Does any body know if it's possible to hook something into the Kernel
(custom module) maybe? My thinking is maybe something that can "catch"
signals on the kernel level. Thanks again :-) |