#!/bin/sh # # autostart. Start a process and monitor it by sending signal 0 to the # the pid listed in pidfile. If it's not responding, restart # # to manually restart the process without restarting autostart and # avoiding race conditions: # # grab the pid from /var/run/xxx.pid # remove /var/run/xxx.pid # kill/restart # # maf - May 1999 while getopts "c:m:p:r:R:s:" option; do case $option in c) # command COMMAND="${OPTARG}";; m) # monitor existing proc, don't startup MON="${OPTARG}";; p) # pidfile PIDFILE="${OPTARG}";; r) # restart if file is newer than pidfile RESTART="${OPTARG}";; R) # restart sleeptime RESTART_SLEEPTIME="${OPTARG}";; s) # sleep time SLEEPTIME="${OPTARG}";; *) # exit 1;; esac done if [ "X${SLEEPTIME}" = X ]; then SLEEPTIME=60 fi if [ "X${RESTART_SLEEPTIME}" = X ]; then RESTART_SLEEPTIME=60 fi if [ "X${PIDFILE}" = X ]; then echo Must specify pidfile with -p 1>&2 exit 1; fi if [ "X${COMMAND}" = X ]; then echo Must specify command with -c 1>&2 exit 1; fi if [ "X${MON}" = "X" ]; then logger -p daemon.notice "$COMMAND started" $COMMAND & sleep $RESTART_SLEEPTIME fi while :; do sleep $SLEEPTIME while [ ! -f $PIDFILE ]; do logger -p daemon.notice "$PIDFILE missing" sleep 5 done # auto start if config file is newer? if [ "X${RESTART}" != "X" ]; then if [ $RESTART -nt $PIDFILE ]; then logger -p daemon.notice "terminating $COMMAND" kill -9 `cat $PIDFILE` 2>/dev/null fi fi kill -0 `cat $PIDFILE` 2>/dev/null && continue logger -p daemon.notice "restarting $COMMAND" $COMMAND & # give some extra startup time sleep $RESTART_SLEEPTIME done