My "Bread and Butter" Process Keep Alive Perl Script....

For the newbies, I should have posted this years ago....

Here is the standard (tiny) "bread and butter" perl script (on Linux) I use in my crontab files to insure key processes are alive ( just in case ! ) like httpd, named, sshd, etc.

The example below if for named......

#!/usr/bin/perl

open(PS,"/bin/ps x|") || die "Can't Open PS";
while(<PS>) {
if (/usr\/sbin\/named/) { close PS; exit;}
}
close PS;
system("/usr/sbin/named");

One of my biggest "server fears" is that named will die for some strange reason, so I feel safe knowing that named is always running with the script above.

New Linux/UNIX users might find it useful.

Now the haute cuisine:
a shell daemon that polls /proc every 30 seconds:

#!/bin/bash

# name of the daemon to watch
pname=named
pstart=/usr/sbin/$pname

# this script
me=${0##*/}
# e.g. me=named_watcher.sh

export PATH=/bin:/usr/bin:/usr/sbin:/sbin

# sh sends SIGHUP on exit, we need to continue
trap 'echo "SIGHUP captured"' HUP

# no interactions, log all output and errors
mkdir -p /var/log
exec </dev/null >/var/log/${me%.*}.log 2>&1

# Solaris needs -z option
zopt=`{ zonename; } 2>/dev/null`
zopt=${zopt:+"-z $zopt"}

echo "$me started with pid $$"
sleep 3
# initiate the pid variables
pid=`pgrep $zopt -x $pname`
[ -n "$pid" ] || exec echo "no $pname process - exiting"
echo "$me watching $pname[$pid]"

# endless loop
while :
do
 sleep 30
 if [ \! -d /proc/$pid ]; then
  echo "`date` $me $pid is gone!"
  sleep 1
  echo "starting $pname"
  date
  eval $pstart
  sleep 3
  # renew the pid variables
  pid=`pgrep $zopt -x $pname`
  [ -n "$pid" ] || exec echo "no $pname process - exiting"
  echo "$me watching $pname[$pid]"
 fi
done

The script should be background-started (with &) in the start section of the process (here: named) init script, *after* the process is started.
And killed in the stop section of the process start script, *before* the process is killed.