Platinum Partner

Simple Process Watchdog

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 = ;
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");
}


{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}