3 # Generic respawner for daemon processes.
5 # Created: Fri, 16 Apr 2004 17:40:36 +0200 zacchiro
6 # Last-Modified: Fri, 16 Apr 2004 17:40:36 +0200 zacchiro
8 # by --Zack <zack@cs.unibo.it>
10 # Test to see if a daemon process (run via /etc/init.d/script) if still alive,
11 # if not it respawns it using the corresponding init script. In order to check
12 # if the daemon is still alive different predicates could be used:
13 # 1) "ps" check with its pid
14 # 2) http request to its url (if it's a web services)
15 # all the available predicated can be enable or not (command line choice), all
16 # the enabled predicates are ANDed. It's enough that one of them fails to
17 # trigger daemon respawning.
19 # This respawner is supposed to be executed by "start" target of daemon's init.d
22 # Sample /etc/init.d/foo script:
24 # DAEMON="/usr/sbin/foo"
25 # PIDFILE="/var/run/$DAEMON.pid"
27 # echo -n "Starting $DAEMON"
29 # --start --background --pidfile $PIDFILE --make-pidfile --exec $DAEMON
31 # echo -n "Starting $DAEMON respawner"
32 # /etc/init.d/daemon_respawner.sh -p $PIDFILE \ -m root@localhost \
33 # -r http://localhost:9999/help -d `basename $0` &
37 # echo -n "Stopping $DAEMON respawner"
38 # /etc/init.d/daemon_respawner.sh -d `basename $0` -s
40 # echo -n "Stopping $DAEMON"
41 # start-stop-daemon --stop --pidfile $PIDFILE
49 TEMP=`getopt -o p:r:d:i:m:s --long pidfile:request:daemon:interval:mailto:stop -- "$@"`
51 echo "Usage: ./daemon_respawner [-p|--pidfile <pidfile>] [-r|--request <url>] [-i|--interval <interval>] [-m|--mailto <mail>] -d|--daemon <daemon>"
52 echo " ./daemon_respawner -d|--daemon <daemon> -s|--stop"
64 -p|--pidfile) PIDFILE="$2"; shift 2 ;;
65 -r|--request) REQUEST="$2"; shift 2 ;;
66 -d|--daemon) DAEMON="$2"; shift 2 ;;
67 -i|--interval) INTERVAL="$2"; shift 2 ;;
68 -m|--mailto) MAILTO="$2"; shift 2 ;;
69 -s|--stop) STOP="yes"; shift ;;
73 if [ -z "$DAEMON" ]; then
74 echo "No daemon provided: aborting."
77 MYPIDFILE="/var/run/$DAEMON""_respawner.pid"
78 if ! [ -z "$STOP" ]; then # stop an active respawner and exit
79 if [ -r "$MYPIDFILE" ]; then
80 kill `cat "$MYPIDFILE"`
85 if [ -z "$PIDFILE" -a -z "$REQUEST" ]; then
86 echo "Neither pidfile nor request URL was provided: aborting."
90 TIMEOUT="5" # timeout for http requests
92 # usage: alert <subject> <body>
95 if [ -z "$MAILTO" ]; then
100 echo "$2" | mail -s "$1" $MAILTO
104 # check if daemon is still alive
108 if ! [ -z "$PIDFILE" ]; then # pid check enabled
109 if ! (ps `cat $PIDFILE` &> /dev/null); then # pid is no longer alive
113 if ! [ -z "$REQUEST" ]; then # request check enabled
114 if ! (wget -T $TIMEOUT -O /dev/null "$REQUEST" &> /dev/null); then
126 invoke-rc.d $DAEMON stop
127 invoke-rc.d $DAEMON start &
128 exit 0 # the respawner will be restarted by daemon's init.d script
133 if ! daemon_is_alive; then
134 alert "$DAEMON failed to start :-((" "$DAEMON died during initialization :-((, enjoy debugging! :-P. Cheers."
138 echo $$ > "$MYPIDFILE"
142 if ! daemon_is_alive; then
143 alert "$DAEMON died :-(, restarting it ..." "$DAEMON died miserably :-(. I'm going to try restarting it, you will receive an additional mail in case of failure. Cheers."
144 start_daemon # performed in background