pl_mom -- cleans up your mess
[mom.git] / pl_mom
1 #!/bin/sh
2 #
3 # chkconfig: 345 98 02
4 # description: pl_mom (daemon of death) startup script
5 #
6
7 CODE='/usr/local/planetlab/bin/pl_mom.pl'
8 PROC='pl_mom'
9
10 . /etc/rc.d/init.d/functions
11
12 RETVAL=0
13
14 pidfile=/var/run/$PROC.pid
15
16 check_status() {
17     pid=`cat $pidfile 2>/dev/null`
18     #
19     # this eliminates a race condition between checking existence of pidfile
20     # and reading its value
21     #
22     [ -n "$pid" -a -d /proc/$pid ]
23 }
24
25 case "$1" in
26     start)
27         echo -n "starting $PROC:"
28         [ -r $CODE ] || action "code missing" /bin/false || exit 1
29         pid=`cat $pidfile 2>/dev/null`
30         if [ -n "$pid" ]; then
31             # check whether process really exists
32             # yes - don't try to start
33             [ -d /proc/$pid ] && action "already running" /bin/true && exit 1
34
35             # no - PID file is stale
36             rm -f $pidfile
37         fi
38
39         $CODE
40         sleep 1
41
42         cmd=success
43         check_status || cmd=failure
44         $cmd "$PROC startup"
45         echo
46         ;;
47
48     stop)
49         echo -n "shutting down $PROC: "
50         check_status && kill -TERM -`cat $pidfile` && sleep 1
51         cmd=failure
52         check_status || cmd=success && rm -f $pidfile
53         $cmd "$PROC shutdown"
54         RETVAL=0
55         echo
56         ;;
57
58     restart|reload)
59         $0 stop
60         $0 start
61         RETVAL=$?
62         ;;
63
64     status)
65         check_status && echo 'running' && exit 0 || \
66             echo 'not running' && exit 1
67         ;;
68
69     *)
70         echo "Usage: $0 {start|stop|restart|status}"
71         exit 1
72 esac
73
74 exit $RETVAL