Sean’s Obsessions


  • I'm a happy Linode customer. This is a Linode 540 VPS. Linode periodically increases RAM and disk while keeping costs steady, which makes them the natural choice.
  • Archives

26 Sep

Process Watchdog

I’m sure there’s a million variants out there, but I had a simple need to make sure a process was running and restart it if it isn’t. I’ve done this before using one off scripts, but then I figured I may as well do one that is generic:

watchdog.sh:

#!/bin/sh

# watchdog LOOKFOR SERVICE
# will restart SERVICE (/sbin/service SERVICE restart) when LOOKFOR doesn’t appear in the processlist

P=`echo $1 | sed ‘s/^\(.\)/[\1]/’` # enclose first char in brackets

ps -ef | grep -v $0 | grep -q “$P” # skip myself

if [ $? -eq 1 ]; then
/sbin/service $2 restart
fi

So,

/usr/local/sbin/watchdog.sh asterisk asterisk

will run /sbin/service asterisk if there are no asterisk processes running.

3 Responses to “Process Watchdog”

  1. 1
    john Says:

    And what watches the watchdog?

    /usr/local/sbin/watchdog.sh watchdog watchdog

    heh.. =)

  2. 2
    sean Says:

    Smartass! :) Run it through inittab if you’re worried, at least if init fails you’ve got bigger things to worry about!

  3. 3
    Rich Says:

    Sean, thanks for the simple yet effective script.

    Thought I would post a warning for users, however. If you use this in a cronjob, do NOT put the name of the process you are looking for in the name of the cron. This will cause a false positive, and the script will not know that your server has gone away.

    For example, I had a cron job named “watch_httpd”, and I couldnt figure out why when i would run the commands manually it worked perfect, but if I ran the cron, it wouldnt work. Well, it saw the process named “watch_httpd” and thought the server was up.

    I’m ashamed to say it took me 2 minutes to implement this script, and an hour to figure out the above issue.

    Thanks again!

Leave a Reply

© 2012 Sean’s Obsessions | Entries (RSS) and Comments (RSS)

Powered by Wordpress, design by Web4Sudoku, based on Pinkline byGPS Gazette