Operating System - HP-UX
1819919 Members
2271 Online
109607 Solutions
New Discussion юеВ

Shell script to restart service automatically

 
SOLVED
Go to solution
Cahyo Tri Nugroho
Occasional Advisor

Shell script to restart service automatically

Dear,

Is there anybody have shell script that can check running services and automatically start the service if it goes down?
TIA.

Regards,
Cahyo
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Shell script to restart service automatically

Shalom Cahyo,

Here is an example

export UNIX95=1
smproc=$(ps -C sendmail | wc -l)

if [ $smproc -le 0 ]
then
/sbin/init.d/sendmail start
fi

You have to write it yourself for the service you wish to monitor.

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
Peter Godron
Honored Contributor

Re: Shell script to restart service automatically

Cahyo,
assuming you want to not test once, schedule Steven's script via cron to run at specified intervals. See man cron and man crontab

And/or see Steven's loop solution at:
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=1024003
spex
Honored Contributor

Re: Shell script to restart service automatically

Steven,

I believe "if [ $smproc -le 0 ]" should be "if [ $smproc -le 1 ]", as 'ps' also returns column headings:

# UNIX95= ps -C foo123xyz | wc -l
1

Cahyo,

Another (less elegant) option:

# while :; do sleep 10; /sbin/init.d/your_daemon start; done > /dev/null 2>&1 &

'nohup' will allow it to persist after your session terminates.

PCS

Yogeeraj_1
Honored Contributor

Re: Shell script to restart service automatically

hi cahyo,

one simple and clean solution would be to use cron to run the check script at regular interval.

e.g. Cron entry that will check your service every 15 minutes.

15 * * * * /users/scripts/check_service.sh 1>/users/scripts/logfiles/output-checkscript.crn 2>/users/scripts/logfiles/error-checkscript.crn
#
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************

hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Bill Hassell
Honored Contributor
Solution

Re: Shell script to restart service automatically

A couple of things about using ps to restart a specific process:

> export UNIX95=1
> smproc=$(ps -C sendmail | wc -l)
> if [ $smproc -le 0 ]
> then
> /sbin/init.d/sendmail start
> fi

It's best never to export the UNIX95 variable, even in a script. UNIX95 enables more options in ps but also affects several other commands and libraries. Use UNIX95 as a temporary variable on the command line as in:

UNIX95=1 ps -C sendmail

Now to eliminate ps headers, use the -o option as in:

UNIX95=1 ps -C sendmail -o pid= -o args=

This eliminates the header line and makes the count correct. So the script would look like this:

MYPROC=sendmail
COUNT=$(UNIX95=1 ps -C $MYPROC -o pid= -o args= | wc -l)
if [ $COUNT -lt 1 ]
then
/sbin/init.d/$MYPROC start
fi

Now the above script is only an example. For instance, if a process is unexpectedly not running, there may be some cleanup tasks that must be accomplished first. With any automated 'fix' for a broken program, you need to investigate why the process is stopping unexpectedly and eliminate the problem rather than kickstarting it from cron.


Bill Hassell, sysadmin
Cahyo Tri Nugroho
Occasional Advisor

Re: Shell script to restart service automatically

Dear all,
Thank you for your help :)

Rgds,
Cahyo