Created
July 13, 2016 19:54
-
-
Save wparad/8c2c2c776fcbede16f00dd079fd84156 to your computer and use it in GitHub Desktop.
Template for init.d service management script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
USER="root" | |
APPLICATION_NAME='{{application_name}}' | |
APP_DIR="`npm config get prefix`/lib/node_modules/${APPLICATION_NAME}" | |
LOG_DIR="/var/log" | |
LOG_FILE="${LOG_DIR}/${APPLICATION_NAME}.log" | |
NODE_EXEC=$(which npm) | |
############### | |
### BEGIN INIT INFO | |
# Provides: $APPLICATION_NAME | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: start and stop $APPLICATION_NAME | |
# Description: $APPLICATION_NAME | |
### END INIT INFO | |
############### | |
USAGE="Usage: $0 {start|stop|restart|status} [--force]" | |
FORCE_OP=false | |
get_pid() { | |
echo $(ps -aefw | grep "node .*server.js" | grep -v " grep " | awk '{print $2}') | |
} | |
is_running() { | |
! [ -z "$(get_pid)" ] | |
} | |
start_app() { | |
if [ -z "$(get_pid)" ] | |
then | |
mkdir -p "$LOG_DIR" | |
chown $USER:$USER "$LOG_DIR" | |
echo "Starting node app ..." | |
echo "cd $APP_DIR && npm start 1>$LOG_FILE 2>&1 & echo \$! > /dev/null" | sudo -i -u $USER | |
echo "Node app started." | |
fi | |
} | |
stop_process() { | |
PID=$(get_pid) | |
echo "Killing process $PID" | |
kill -9 $PID | |
} | |
stop_app() { | |
echo "Stopping node app ..." | |
if is_running | |
then | |
stop_process | |
fi | |
echo "Node app stopped." | |
} | |
status_app() { | |
if is_running | |
then | |
PID=$(get_pid) | |
echo "Node app running with pid $PID" | |
else | |
echo "Node app stopped." | |
fi | |
} | |
case "$2" in | |
--force) | |
FORCE_OP=true | |
;; | |
"") | |
;; | |
*) | |
echo $USAGE | |
exit 1 | |
;; | |
esac | |
case "$1" in | |
start) | |
start_app | |
;; | |
stop) | |
stop_app | |
;; | |
restart) | |
stop_app | |
start_app | |
;; | |
status) | |
status_app | |
;; | |
*) | |
echo $USAGE | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment