DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Tim has posted 1 posts at DZone. View Full User Profile

Simple Process Watchdog

03.23.2006
| 10714 views |
  • submit to reddit
        I had a problem where an important monitoring daemon was committing sepuku every night between 3 and 4 am. While debugging it, I whipped up the script below.

Basically, it checks to see if the process ID file is present, and if it is, checks that the process is actually running. If not, it invokes the restart command.

It outputs info to syslog so you can check it out the next day.

Written in perl, requires Sys::Syslog. Tested on Linux (your ps command details may vary on other unices). Not worth bothering with this on windows.

#!/usr/bin/perl

use Sys::Syslog qw(:standard :macros);

my $pidfile = "/usr/local/bs/adm/uxmon.pid";
my $check_command = "uxmon";
my $start_command = "/etc/init.d/bigsister restart";

openlog("ProcessWatcher", "pid",LOG_LOCAL0);

if (! -e $pidfile) {
    syslog(LOG_WARNING, "Process ID file not found, restarting application");
    &restart_proc;
    exit;
}

open (IN, $pidfile) || syslog(LOG_ERR, "Couldn't read $pidfile- $!");
my $pid = <IN>;
chomp $pid;

my $ret = `ps -p $pid -o comm=`;
if ($ret != $check_command) {
    syslog(LOG_ERR, "Return value mismatch, restarting Process");
    &restart_proc;
    exit;
}



sub restart_proc() {
    my $msg = `$start_command`;
    syslog (LOG_INFO, "Restarting Process returned: $msg");
}