1833747 Members
2751 Online
110063 Solutions
New Discussion

Re: shell script

 
SOLVED
Go to solution
Tal Drigov
Advisor

shell script

Hi,

I have a process that needs to be up all times. I would like to do a script that when this process dies the server will do a reboot.
How can I do that ?

Regards,
Tal.
9 REPLIES 9
Ross Zubritski
Trusted Contributor

Re: shell script

Tal,

Why would you reboot a box when a process dies? Why would you not respawn the process?

Regards.

RZ
John Poff
Honored Contributor

Re: shell script

Hi,

You could write a script and put the entry into the /etc/inittab file with an action of 'respawn', which start the script at boot [depending on the run level you specify] and restart the script if it dies. Rebooting the box is pretty hard core, but I guess it could be done. You can't live with just restarting the process?

JP
Tal Drigov
Advisor

Re: shell script

Because the process that for some unknown reason dies on me from time to time is dhcpclient, and you can't start it manually.
Or can you ?
Chris Vail
Honored Contributor
Solution

Re: shell script

This isn't really effective systems administration, but its your system. Here's a quick script. To find the name of your running application, do "ps -ef|awk '{ print $9 }'|pg" until you see your application. Put that name on the right side of the = sign next to APP, below. As long as your application has a Process ID, your system won't reboot.

#!/bin/ksh
APP=yourapplication

while true
do
PID=`ps -ef|grep $APP|awk '{ print $3 }'`
if test $PID
then
sleep 60 #or some other number, in seconds
else
reboot
fi
done

Its a much better idea to find out why your application is dying, and fix it.

Chris
Steven E. Protter
Exalted Contributor

Re: shell script

/etc/init.d/dhcpd restart


service restart dhcpd


That will restart the server daemon.


Surely there is a command for root user that restarts the client process.

Reboot is not the answer.

Trapping, tracking and fixing the error is harder but the right way to go.

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
S.K. Chan
Honored Contributor

Re: shell script

The command is in /usr/lbin ..
and you got to take a look at the current options you had setup for dhcpclient at startup in order to run the command exactly with the same options.
# /usr/lbin/dhcpclient -h
would give brief usage.
John Dvorchak
Honored Contributor

Re: shell script

You can get dhcpclient to start manually and maintain a lease.

dhcpclient -m lan0

or whatever lanx number you want to maintaint the lease. Dchpclient renews the lease then calculates the time that it needs to sleep before it wakes up and tries to renew the lease again. Usually dhcp clients need to try a renew half way through the lease period.

man dhcpclient

Good luck.
If it has wheels or a skirt, you can't afford it.
Tal Drigov
Advisor

Re: shell script

Hi chris,

how can I find out why the process is dying all the time ?

T.
John Dvorchak
Honored Contributor

Re: shell script

From the man pages:

To maintain the lease for the interface lan0 and invoke logging in syslog, the following set of commands is invoked at the command prompt:

dhcpclient -b lan0 -N "dhcp1"
dhcpclient -m lan0 -N "dhcp1" -l 3 -t 1
If it has wheels or a skirt, you can't afford it.