[svlug] Process watchdog

Brian J. Tarricone bjt23 at cornell.edu
Fri May 2 14:23:06 PDT 2008


Tomer wrote:
> Hi all
> Anyone know an easy way to place a watchdog deamon that will monitor a
> process being alive and if not, restart it? E.g. I have tomcat running and
> it sometime dies off. I want some deamon that will find out the tomcat
> process (Actually java) is no longer running and then relaunch it. 
> 
> Even a short script with ps -ef and grepping inside a constant loop would do
> if you have it.

Off the top of my head; untested:

#!/bin/bash
pidfile=/var/run/tomcat.pid
for((;;)); do
     if [ ! -f $pidfile ] || ! kill -0 `cat $pidfile`; then
         /etc/init.d/tomcat restart  # or whatever
     fi
     sleep 5
done

If tomcat doesn't create a pidfile, you could do `pidof tomcat` or 
something like `ps -e | grep tomcat | head -n 1 | awk '{ print $1 }'` 
instead.

(If your bash doesn't support 'for((;;))' syntax for some reason, you 
can always use 'while true'.)

	-brian



More information about the svlug mailing list