CORE-POS/IS4C

View on GitHub
pos/is4c-nf/scale-drivers/drivers/NewMagellan/posdriver-sph

Summary

Maintainability
Test Coverage
#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          sph
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop sph scanner-scale driver
### END INIT INFO
#
# posdriver-sph Startup script for pos.exe
#
# chkconfig: - 80 20
# description: runs pos.exe scale driver as a daemon
# IMPORTANT: $DRIVER to include correct path

# --COMMENTS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#  6May2015 AT Revised to be less Red Hat specific
#              Added status option. Seems to manage PID file
#              and process status correctly.
#  2Nov2012 EL Add LSB header, needed to start at boot.
#              Also needs: update-rc.d podriver-sph defaults
# 26Oct2012 Eric Lee Change path to driver.
#                    For Debian systems, posdriver-sph-debian may be better.

# --MAIN - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if [[ -f /lib/lsb/init-functions ]]; then
    . /lib/lsb/init-functions
elif [[ -f /etc/init.d/functions ]]; then
    . /etc/rc.d/init.d/functions
    function start_daemon() {
        daemon $*
    }
fi

NAME=posdriver-sph
DRIVER="/var/www/IS4C/pos/is4c-nf/scale-drivers/drivers/NewMagellan/pos.exe"
MONO=/usr/bin/mono
RUNDIR="/var/run/$NAME"
OPTS="-v"

mkdir -p $RUNDIR

start(){
    echo "Starting $NAME"
    if [[ -f "$RUNDIR/pos.pid" && -d "/proc/`cat ${RUNDIR}/pos.pid`" ]]; then
        echo "Already running!";
        log_failure_msg;
    else 
        nohup $MONO $DRIVER $OPTS &> "$RUNDIR/pos.log" & 
        MYPID=$!
        echo $MYPID > "$RUNDIR/pos.pid"
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            log_success_msg;
        else
            log_failure_msg;
        fi
    fi
    echo
}

stop(){
    echo "Stopping $NAME"
    killproc -p "$RUNDIR/pos.pid" $MONO
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        log_success_msg;
    else
        log_failure_msg;
    fi
    echo
}

status(){
    if [[ -f "$RUNDIR/pos.pid" && -d "/proc/`cat ${RUNDIR}/pos.pid`" ]]; then
        echo "$NAME is running"
    elif [ -f "$RUNDIR/pos.pid" ]; then
        echo "$NAME may have crashed"
    else 
        echo "$NAME is stopped"
    fi
}

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

exit 0