tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

cron script to monitor whether daemons are running



Sometimes essential daemons die when they should not. Without a
parent watcher process of some kind constantly lying in wait(2),
someone has to notice and restart them. That is the genesis of the
following "keepalive" shell script. It may be of some use to some
of you.

        Erik <fair%netbsd.org@localhost>

#!/bin/sh
# Monitor the existence of daemons with .pid files and restart as necessary.
#
# This script depends on the NetBSD convention that a daemon, its
# PID file, and its rc.d(8) control script all have the same name,
# and that it will be run as root by cron(8) as frequently as the
# system administrator deems necessary, e.g.
# */5  *  *  *  *  /usr/local/sbin/keepalive named ntpd sshd
#
# Erik Fair <fair%netbsd.org@localhost>, Nov 26 2013

PIDdir=/var/run

cd /tmp

for daemon do
        if [ -d ${PIDdir}/${daemon} ]; then
                PIDfile=${PIDdir}/${daemon}/${daemon}.pid
        else
                PIDfile=${PIDdir}/${daemon}.pid
        fi

        if [ -f ${PIDfile} ]; then
                if shlock -f ${PIDfile}; then
#                       echo ${daemon} is alive
                else
#                       echo ${daemon} is dead
                        /etc/rc.d/${daemon} restart
                fi
        else
                echo ${daemon} has no ${PIDfile}
        fi
done


Home | Main Index | Thread Index | Old Index