This commit was manufactured by cvs2svn to create branch
[mom.git] / pl_mom
diff --git a/pl_mom b/pl_mom
new file mode 100755 (executable)
index 0000000..a41c481
--- /dev/null
+++ b/pl_mom
@@ -0,0 +1,74 @@
+#!/bin/sh
+#
+# chkconfig: 345 98 02
+# description: pl_mom (daemon of death) startup script
+#
+
+CODE='/usr/local/planetlab/bin/pl_mom.pl'
+PROC='pl_mom'
+
+. /etc/rc.d/init.d/functions
+
+RETVAL=0
+
+pidfile=/var/run/$PROC.pid
+
+check_status() {
+    pid=`cat $pidfile 2>/dev/null`
+    #
+    # this eliminates a race condition between checking existence of pidfile
+    # and reading its value
+    #
+    [ -n "$pid" -a -d /proc/$pid ]
+}
+
+case "$1" in
+    start)
+       echo -n "starting $PROC:"
+       [ -r $CODE ] || action "code missing" /bin/false || exit 1
+       pid=`cat $pidfile 2>/dev/null`
+       if [ -n "$pid" ]; then
+           # check whether process really exists
+           # yes - don't try to start
+           [ -d /proc/$pid ] && action "already running" /bin/true && exit 1
+
+           # no - PID file is stale
+           rm -f $pidfile
+       fi
+
+       $CODE
+       sleep 1
+
+       cmd=success
+       check_status || cmd=failure
+       $cmd "$PROC startup"
+       echo
+        ;;
+
+    stop)
+       echo -n "shutting down $PROC: "
+       check_status && kill -TERM -`cat $pidfile` && sleep 1
+       cmd=failure
+       check_status || cmd=success && rm -f $pidfile
+       $cmd "$PROC shutdown"
+       RETVAL=0
+       echo
+       ;;
+
+    restart|reload)
+       $0 stop
+       $0 start
+       RETVAL=$?
+        ;;
+
+    status)
+       check_status && echo 'running' && exit 0 || \
+           echo 'not running' && exit 1
+       ;;
+
+    *)
+       echo "Usage: $0 {start|stop|restart|status}"
+       exit 1
+esac
+
+exit $RETVAL