Skip to content

Instantly share code, notes, and snippets.

@orrsella
Last active August 2, 2020 05:43
Show Gist options
  • Select an option

  • Save orrsella/6ccaa03886bc857e00e4 to your computer and use it in GitHub Desktop.

Select an option

Save orrsella/6ccaa03886bc857e00e4 to your computer and use it in GitHub Desktop.

Revisions

  1. orrsella revised this gist Mar 20, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions init.d-script.sh
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ case "$1" in
    start)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    if [ -z "`ps axf | grep -w ${PID} | grep -v grep`" ]; then
    start
    else
    echoYellow "Already running [$PID]"
    @@ -50,7 +50,7 @@ start)
    status)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    if [ -z "`ps axf | grep -w ${PID} | grep -v grep`" ]; then
    echoRed "Not running (process dead but pidfile exists)"
    exit 1
    else
    @@ -66,7 +66,7 @@ status)
    stop)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    if [ -z "`ps axf | grep -w ${PID} | grep -v grep`" ]; then
    echoRed "Not running (process dead but pidfile exists)"
    exit 1
    else
  2. orrsella created this gist Mar 20, 2016.
    93 changes: 93 additions & 0 deletions init.d-script.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #!/bin/bash

    START_SCRIPT={{ start_script }}
    PID_FILE={{ pid_file }}

    # ***********************************************
    # ***********************************************

    ARGS="" # optional start script arguments
    DAEMON=$START_SCRIPT

    # colors
    red='\e[0;31m'
    green='\e[0;32m'
    yellow='\e[0;33m'
    reset='\e[0m'

    echoRed() { echo -e "${red}$1${reset}"; }
    echoGreen() { echo -e "${green}$1${reset}"; }
    echoYellow() { echo -e "${yellow}$1${reset}"; }

    start() {
    PID=`$DAEMON $ARGS > /dev/null 2>&1 & echo $!`
    }

    case "$1" in
    start)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    start
    else
    echoYellow "Already running [$PID]"
    exit 0
    fi
    else
    start
    fi

    if [ -z $PID ]; then
    echoRed "Failed starting"
    exit 3
    else
    echo $PID > $PID_FILE
    echoGreen "Started [$PID]"
    exit 0
    fi
    ;;

    status)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    echoRed "Not running (process dead but pidfile exists)"
    exit 1
    else
    echoGreen "Running [$PID]"
    exit 0
    fi
    else
    echoRed "Not running"
    exit 3
    fi
    ;;

    stop)
    if [ -f $PID_FILE ]; then
    PID=`cat $PID_FILE`
    if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
    echoRed "Not running (process dead but pidfile exists)"
    exit 1
    else
    PID=`cat $PID_FILE`
    kill -HUP $PID
    echoGreen "Stopped [$PID]"
    rm -f $PID_FILE
    exit 0
    fi
    else
    echoRed "Not running (pid not found)"
    exit 3
    fi
    ;;

    restart)
    $0 stop
    $0 start
    ;;

    *)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
    esac