Skip to content

Instantly share code, notes, and snippets.

@voyager123bg
Forked from Radamanf/Ghost - Demonize anything
Last active March 2, 2018 14:41
Show Gist options
  • Save voyager123bg/5dc288ff8944ff75f2cb2ee8f7d5ac1d to your computer and use it in GitHub Desktop.
Save voyager123bg/5dc288ff8944ff75f2cb2ee8f7d5ac1d 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 y…
# 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
# Slightly altered, Author: Nikolay Georgiev.
if [ -z "$2" ]; then
echo "You must specify microservice-name to use this script!"
exit 1
fi
# For debugging purposes uncomment next line
#set -x
# Config, can be altered
APP_NAME="$2"
APP_FILENAME=`ls -1 ...| tail -1`
APP_PID=".../$APP_NAME/PID"
APP_PATH=".../$APP_NAME"
APP_LOGS=$APP_PATH"/logs"
#for example: APP_PRE_OPTION="java -jar"
APP_PRE_OPTION="java -Dlog4j.configurationFile=${APP_PATH}/etc/log4j2.xml -jar "
APP_POST_OPTION=""
APP_SUCCESS_START_MSG="..."
TIMESTAMP=`date "+%Y-%m-%d-%H-%M"`
APP_START_LOG="${APP_NAME}.${TIMESTAMP}.out"
STATUS_CODE[0]="Is Running"
STATUS_CODE[1]="Not Running"
start() {
checkpid
STATUS=$?
if [ $STATUS -ne 0 ] ;
then
echo "Starting $APP_NAME..."
cd ${APP_PATH}
echo "Executing: nohup ${APP_PRE_OPTION} ${APP_FILENAME} ${APP_POST_OPTION} > ${APP_LOGS}/${APP_START_LOG} 2> ${APP_LOGS}/${APP_NAME}.err < /dev/null &"
nohup $APP_PRE_OPTION $APP_FILENAME $APP_POST_OPTION > $APP_LOGS/$APP_START_LOG 2> $APP_LOGS/$APP_NAME.err < /dev/null &
echo PID $!
echo $! > $APP_PID
JPPID=`cat $APP_PID`
sleep 15
echo grep "'${APP_SUCCESS_START_MSG}'" $APP_LOGS/$APP_START_LOG
grep "'${APP_SUCCESS_START_MSG}'" $APP_LOGS/$APP_START_LOG
if [ ! $? ]; then
echo $APP_SUCCESS_START_MSG " not found in output. Aborting execution."
kill -9 $JPPID
exit 1
fi
if ! ps -p $JPPID >/dev/null ; then
wait $JPPID
echo " PID: $JPPID Exited with: $? "
tail -n 100 $APP_LOGS/$APP_START_LOG
fi
statusit
#echo "Done"
else
echo "$APP_NAME Already Running"
fi
}
stop() {
checkpid
STATUS=$?
if [ $STATUS -eq 0 ] ;
then
echo "Stopping $APP_NAME..."
APPPID=`ps -ef | grep $APP_NAME |grep -v control | grep -v grep | awk '{print $2}'`
kill -15 $APPPID
rm $APP_PID
sleep 10
checkpid
STATUS=$?
if [ $STATUS -eq 0 ] ; then
echo "Hard kill!"
kill -9 $APPPID
fi
statusit
#echo "Done"
else
echo "$APP_NAME - Already killed"
fi
}
checkpid(){
ps -ef | grep $APP_NAME | grep -v control.sh | grep -v grep | awk '{print $2}' >$APP_PID
ps -ef | grep $APP_NAME | grep -v control.sh | grep -v grep
STATUS=$?
return $STATUS
}
statusit() {
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 $APP_NAME - $EXITSTATUS
}
case "$1" in
'start')
start
;;
'stop')
stop
#statusit
;;
'restart')
stop
start
;;
'status')
statusit
;;
*)
echo "Usage: $0 { start | stop | restart | status } microservice-name"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment