CatTail
4/20/2016 - 10:29 AM

Simple process manager

Simple process manager

#! /bin/bash

PROGRAM_NAME="choppe"
PROGRAM="node app.js"

function getpid()
{
    ps aux | grep "$PROGRAM" | grep -v grep | awk '{ print $2 }'
}

function help()
{
    echo "Usage: bin/ctl <start|stop|restart|status>"
}

function start()
{
    if [ $(getpid) ]; then
        echo "$PROGRAM_NAME already running"
    else
        nohup $PROGRAM&
        echo "$PROGRAM_NAME started"
    fi
}

function stop()
{
    if [ $(getpid) ]; then
        kill -s SIGKILL $(getpid)
        echo "$PROGRAM_NAME stoped"
    else
        echo "$PROGRAM_NAME not running"
    fi
}

case $1 in
    start )
        start
        ;;
    stop )
        stop
        ;;
    restart )
        stop
        start
        ;;
    status )
        if [ $(getpid) ]; then
            echo "$PROGRAM_NAME is running"
        else
            echo "$PROGRAM_NAME is not running"
        fi
        ;;
    help|-h|--help )
        help
        ;;
    * )
        help
        ;;
esac