<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Thowland's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:23:40 GMT</pubDate>
    <description>DZone Snippets: Thowland's Code Snippets</description>
    <item>
      <title>Simple process watchdog</title>
      <link>http://snippets.dzone.com/posts/show/1737</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;It outputs info to syslog so you can check it out the next day.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;use Sys::Syslog qw(:standard :macros);&lt;br /&gt;&lt;br /&gt;my $pidfile = "/usr/local/bs/adm/uxmon.pid";&lt;br /&gt;my $check_command = "uxmon";&lt;br /&gt;my $start_command = "/etc/init.d/bigsister restart";&lt;br /&gt;&lt;br /&gt;openlog("ProcessWatcher", "pid",LOG_LOCAL0);&lt;br /&gt;&lt;br /&gt;if (! -e $pidfile) {&lt;br /&gt;    syslog(LOG_WARNING, "Process ID file not found, restarting application");&lt;br /&gt;    &amp;restart_proc;&lt;br /&gt;    exit;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;open (IN, $pidfile) || syslog(LOG_ERR, "Couldn't read $pidfile- $!");&lt;br /&gt;my $pid = &lt;IN&gt;;&lt;br /&gt;chomp $pid;&lt;br /&gt;&lt;br /&gt;my $ret = `ps -p $pid -o comm=`;&lt;br /&gt;if ($ret != $check_command) {&lt;br /&gt;    syslog(LOG_ERR, "Return value mismatch, restarting Process");&lt;br /&gt;    &amp;restart_proc;&lt;br /&gt;    exit;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sub restart_proc() {&lt;br /&gt;    my $msg = `$start_command`;&lt;br /&gt;    syslog (LOG_INFO, "Restarting Process returned: $msg");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 23 Mar 2006 19:20:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1737</guid>
      <author>thowland (Tim Howland)</author>
    </item>
  </channel>
</rss>
