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

  • 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.


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

my $pidfile = "/usr/local/bs/adm/";
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");

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

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