#!/bin/bash
#
# batchcompute: the shell script takes care of batch_compute_agent auto start and stop
# chkconfig: 2345 20 80
# description: batch_compute_agent 

SERVICE_NAME=sge_master_watchdog
EXEC=sge_master_watchdogd
DAEMON=/usr/local/bin/$EXEC
PID_FILE=/var/run/$EXEC.pid

if ! [ -x $DAEMON ] ; then
    echo "ERROR: $DAEMON not found"
    exit 1
fi

stop()
{
    echo "Stoping $EXEC ..."
    killall $EXEC >/dev/null
    sleep 1
    echo "Shutting down $EXEC: [  OK  ]"     
}

start()
{
    echo "Starting $EXEC ..."
    cd /var/log
    . /usr/share/gridengine/default/common/settings.sh
    $DAEMON > /dev/null &
    sleep 1
    echo "Starting $EXEC: [  OK  ]"        
}

restart()
{
    stop
    start
}

status()
{
    # if [ -f $PID_FILE ] ; then
    #     PID=`cat $PID_FILE`
    #     if [ "`ps axf | grep ${PID} | grep -v grep`" ] ; then
    #         echo "$EXEC is running"
    #         exit 0
    #     fi
    # fi
    # echo "$EXEC is stopped"
    if [ "`ps axf | grep ${DAEMON} | grep -v grep`" ] ; then
        echo "$EXEC is running"
    else
        echo "$EXEC is stopped"
    fi
}


case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    restart
    ;;
    status)
    status
    ;;   
    *)
    echo "Usage: service $SERVICE_NAME {start|stop|restart|status}"
    exit 1
esac

exit $?
