Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Monitor a process, then restart it if its down....

 
SOLVED
Go to solution
Highlighted
Super Advisor

Monitor a process, then restart it if its down....

Hello,

I have a particlar process that I want to monitor continously and that I want to restart if it stops.

Any ideas?

Thanks,

6 REPLIES 6
Highlighted
Outstanding Contributor

Re: Monitor a process, then restart it if its down....

Perhaps a cron or at job that runs every minute, checks for the presence of said process and re-initiates it if absent?


Pete

Pete
Highlighted
Exalted Contributor

Re: Monitor a process, then restart it if its down....

Shalom,

Lets use sendmail as an example.

Script stubb


procname=sendmail
UNIX95=1

while true
do
sprocs=$(ps -C $procname)
if [ $sprocs -eq 0 ]
then
/sbin/init.d/sendmail start
fi
sleep 1800
done


This script stub(incomplete script) sees if sendmail is running. It does not use grep, which Bill Hassell says is a no-no.

If the number of sendmail processes is 0 it restarts the daemon.

What it needs is code if it can't restart the daemon. Obviously by changing it, you can monitor any daemon.

Note, if you want to monitor oracle, its unfortuneatly quite possible for there to be a process and the database be hoplessly down. You need sql code to accurately monitor oracle.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Highlighted
Honored Contributor

Re: Monitor a process, then restart it if its down....

If this is a process that runs all the time, you could put an entry in "/etc/inittab". If you set the second parameter to "respawn", then init will re-launch the process if it terminates.

HTH

-- Rod Hills
There be dragons...
Highlighted
Honored Contributor

Re: Monitor a process, then restart it if its down....

Hi,

You can put the process into /etc/inittab with respawn option. RTFM for inittab
HTH
Entities are not to be multiplied beyond necessity - RTFM
Highlighted
Honored Contributor
Solution

Re: Monitor a process, then restart it if its down....

Well we do it much like Steve's post. But I actually start that monitoring script from cron every 15 minutes, then have that script check and see if a copy of itself is already running, if it is, it exits. Otherwise, it proceeds to go into a loop and monitor the process that supposed to be running.

This way, if the program dies, the monitor catches it. If the monitor dies, it is relaunched via cron, and if both die, then the monitor launches, and in turn launches the monitored process.

If I may expand on/change Steve's example; here's the script to have run from cron. Be aware that to make it run from cron, you may have put put in absolute paths for every command in the file (e.g. put in "/usr/bin/ps" instead of "ps").

#!/bin/ksh

export CYCLE_TIME=1800
export PROCNAME=sendmail

# Chk to see if monitor is already running
export MYPROG=`basename $0`
let PROGCOUNT=0
export PROGCOUNT

UNIX95=1
PROGCOUNT=`ps -C ${MYPROG}| wc -l`
UNIX95=0
if [ $PROGCOUNT -gt 2 ]; then
exit 0
fi

# Monitor sendmail
while true
do

UNIX95=1
PROGCOUNT=`ps -C ${PROCNAME}|wc -l`
UNIX95=0
if [ $PROGCOUNT -lt 2 ]; then
/sbin/init.d/sendmail start
fi

sleep $CYCLE_TIME

done
We are the people our parents warned us about --Jimmy Buffett
Highlighted
Super Advisor

Re: Monitor a process, then restart it if its down....

#!/bin/ksh

export CYCLE_TIME=1800
export PROCNAME=sendmail

# Chk to see if monitor is already running
export MYPROG=`basename $0`
let PROGCOUNT=0
export PROGCOUNT

UNIX95=1
PROGCOUNT=`ps -C ${MYPROG}| wc -l`
UNIX95=0
if [ $PROGCOUNT -gt 2 ]; then
exit 0
fi

# Monitor sendmail
while true
do

UNIX95=1
PROGCOUNT=`ps -C ${PROCNAME}|wc -l`
UNIX95=0
if [ $PROGCOUNT -lt 2 ]; then
/sbin/init.d/sendmail start
fi

sleep $CYCLE_TIME

done