Skip to content

Instantly share code, notes, and snippets.

@Radamanf
Last active January 6, 2020 16:04
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save Radamanf/7152723 to your computer and use it in GitHub Desktop.
Save Radamanf/7152723 to your computer and use it in GitHub Desktop.
This is an initialization script "/etc/init.d/ghost" file which helps you to start, stop, status executable or java -jar file.jar with nohup and & - background option, this is pretend way make a daemon, but maybe will be interested to someone. It uses PID file and checks if the process is running before every start or stop. Once it's configured …
#!/bin/bash
# Licence: GPLv3, MIT, BSD, Apache or whatever you prefer; FREE to use, modify, copy, no obligations
# Description: Bash Script to Start the process with NOHUP and & - in background, pretend to be a Daemon
# Author: Andrew Bikadorov
# Script v1.5
# For debugging purposes uncomment next line
#set -x
# Config, can be altered
APP_NAME="Ghost"
APP_FILENAME="ghost"
APP_PID="/var/run/$APP_FILENAME.pid"
APP_PATH="/opt/ghost"
APP_FILE=$APP_FILENAME".jar"
APP_LOGS=$APP_PATH"/logs"
#for example: APP_PRE_OPTION="java -jar"
APP_PRE_OPTION=""
APP_POST_OPTION=""
# Should Not Be altered
TMP_FILE="/tmp/status_$APP_FILENAME"
### For internal usage
STATUS_CODE[0]="Is Running"
STATUS_CODE[1]="Not Running"
STATUS_CODE[2]="Stopped incorrectly"
STATUS_CODE[9]="Default Status, should not be seen"
start() {
checkpid
STATUS=$?
if [ $STATUS -ne 0 ] ;
then
echo "Starting $APP_NAME..."
## java –jar $APP_PATH/ghost.jar
nohup $APP_PRE_OPTION $APP_PATH/$APP_FILE $APP_POST_OPTION > $APP_LOGS/$APP_FILENAME.out 2> $APP_LOGS/$APP_FILENAME.err < /dev/null &
# You can un-comment next line to see what command is exactly executed
# echo "nohup $APP_PRE_OPTION $APP_PATH/$APP_FILE $APP_POST_OPTION > $APP_LOGS/$APP_FILENAME.out 2> $APP_LOGS/$APP_FILENAME.err < /dev/null &"
echo PID $!
echo $! > $APP_PID
statusit
#echo "Done"
else
echo "$APP_NAME Already Running"
fi
}
stop() {
checkpid
STATUS=$?
if [ $STATUS -eq 0 ] ;
then
echo "Stopping $APP_NAME..."
kill `cat $APP_PID`
rm $APP_PID
statusit
#echo "Done"
else
echo "$APP_NAME - Already killed"
fi
}
checkpid(){
STATUS=9
if [ -f $APP_PID ] ;
then
#echo "Is Running if you can see next line with $APP_NAME"
ps -Fp `cat $APP_PID` | grep $APP_FILE > $TMP_FILE
if [ -f $TMP_FILE -a -s $TMP_FILE ] ;
then
STATUS=0
#"Is Running (PID `cat $APP_PID`)"
else
STATUS=2
#"Stopped incorrectly"
fi
## Clean after yourself
rm -f $TMP_FILE
else
STATUS=1
#"Not Running"
fi
return $STATUS
}
statusit() {
#TODO
#status -p $APP_PID ghost
checkpid
#GET return value from previous function
STATUS=$?
#echo $?
EXITSTATUS=${STATUS_CODE[STATUS]}
if [ $STATUS -eq 0 ] ;
then
EXITSTATUS=${STATUS_CODE[STATUS]}" (PID `cat $APP_PID`)"
fi
#echo "First Index: ${NAME[0]}"
#echo "Second Index: ${NAME[1]}"
echo $APP_NAME - $EXITSTATUS
#${STATUS_CODE[STATUS]}
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
statusit
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
exit 1
;;
esac
exit 0
@ravjot28
Copy link

ravjot28 commented Oct 4, 2017

I think this will not kill the nohup process.
Could you please test it for the same?

@KrashLeviathan
Copy link

This was very useful! Except I found that, to use it as an /etc/init.d service, you need to add something similar to the following at the top of the file below the shebang:

### BEGIN INIT INFO
# Provides:          some_service
# Required-Start:    $all
# Required-Stop:
# Default-Start:     3
# Default-Stop:
# Short-Description: Starts the Some Service service
### END INIT INFO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment