Skip to content

Instantly share code, notes, and snippets.

@orrsella
Last active August 2, 2020 05:43
Show Gist options
  • Save orrsella/6ccaa03886bc857e00e4 to your computer and use it in GitHub Desktop.
Save orrsella/6ccaa03886bc857e00e4 to your computer and use it in GitHub Desktop.
Init.d Script
#!/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 -w ${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 -w ${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 -w ${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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment