#!/bin/sh
#
# start/stop the tunnel monitor daemon.
#
### BEGIN INIT INFO
# Provides:          tunnel_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 tunnel monitor daemon.
# Description:       Starts and stops the tunnel monitor daemon
#                    to manage ssh tunnels for job submission
### END INIT INFO

if ! [ -x /opt/submit/monitorTunnel.py ]; then
   exit 1
fi

case "$1" in
    start)
        echo -n "Starting tunnel monitor:"
        echo -n " tunnelmon" ; pgrep -fl '/opt/submit/monitorTunnel.py' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorTunnel.py
        echo "."
        ;;
    stop)
        echo -n "Stopping tunnel monitor:"
        echo -n " tunnelmon" ; pkill -TERM -f '/opt/submit/monitorTunnel.py'
        echo "."
        ;;
    restart|force-reload)
        echo -n "Restarting tunnel monitor:"
        echo -n " tunnelmon"
        pkill -TERM -f '/opt/submit/monitorTunnel.py'
       /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorTunnel.py
        echo "."
        ;;
    *)
        echo "Usage: /etc/init.d/tunnelmon {start|stop|restart|force-reload}"
        exit 1
        ;;
esac

exit 0
