Over a million developers have joined DZone.

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.


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

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

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


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}