Linux.com

Home Linux Community Forums Software Command Line Good watchdog for services?

Good watchdog for services?

Link to this post 22 Apr 09

I have a simple script i use to keep track of services running on some of my servers and restart them if they die. Its pretty easy and its dead fast to implement on a server. I wonder if anyone has a simpler/better way than"my" script to watchdog services and restart them as nessecary?

This is what i use now and call on from /etc/crontab once every five minutes or whatever depending on how critical the service is.

server:~ # cat /usr/bin/service-watchdog
#!/bin/bash

MYPROC=programname

COUNT=$(UNIX95=1 ps -C $MYPROC -o pid= -o args= | wc -l) \
if [ $COUNT -lt 1 ]
then
echo "Service xxx is restarting at 'date' >> /var/log/messages
/etc/init.d/servicename restart
else
echo "Service xxx is running."
fi


programname is whatever the process is called
servicename is what the file for starting the service in /etc/init.d is called.


:woohoo:

Link to this post 22 Apr 09

I wrote a similar script a few years ago that used pgrep. If pgrep returned any value besides 1 (I don't think one would use this for init) then it would exit, otherwise, restart the service.

Link to this post 22 Apr 09

Useful!

Link to this post 22 Apr 09

I'm kindof interested in what kind of apps you are running that need to be constantly restarted? I've done the same type of thing to restart dead services as a 'quick fix', but I would think the best course is to fix whatever is causing the application to restart. :)

Link to this post 22 Apr 09

You may disable the echo commands as they clog up your root's mailbox, and send a mail directly to root once the need of restarting a service arises.

/etc/init.d/servicenme restart 2> /tmp/restart
mail root < /tmp/restart

Link to this post 22 Apr 09

In my case, I was hosting Ventrilo for my WoW guild. It would die occasionally, so I wrote a script that did pgrep vent_srv, and if that returned 1 then I restarted it.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board