#!/bin/sh
#
# start/stop the submit job monitor daemon.
#
### BEGIN INIT INFO
# Provides:          job_monitor
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the submit job monitor daemon.
# Description:       Starts and stops the submit job monitor daemon so that
#                    the submit server knows what remote job status is.
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

MONITORPATH=/opt/submit/monitorJob.py

if ! [ -x ${MONITORPATH} ]; then
   exit 1
fi

case "$1" in
   start)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Starting submit job monitor:"
         echo -n " jobmon" ; pgrep -fl 'python ${MONITORPATH}' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         echo -n " jobmon" ; daemon --check monitorJob --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping submit job monitor:"
      fi
      echo -n " jobmon" ; pkill -TERM -u gridman -f "${MONITORPATH}"
      echo "."
      ;;
    restart|force-reload)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting submit job monitor:"
      fi
      echo -n " jobmon"
      pkill -TERM -u gridman -f "${MONITORPATH}"
      if [ -f /sbin/start-stop-daemon ]; then
         /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         daemon --check monitorJob --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/jobmon {start|stop|restart|force-reload}"
      exit 1
      ;;
esac

exit 0
