Skip to content

Instantly share code, notes, and snippets.

@cbess
Created November 29, 2015 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbess/04fe9dfbb99bd22c62b6 to your computer and use it in GitHub Desktop.
Save cbess/04fe9dfbb99bd22c62b6 to your computer and use it in GitHub Desktop.
Debian nodejs service script
#!/bin/sh
# tested on debian 6.x
NODE_ENV="production"
# the name for the pid and the log file, should be unique for the environment
FNAME="script-name"
APP_DIR="/var/webapps/example.com/src/nodejs"
NODE_APP="server.js"
PORT="7707"
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/$FNAME.pid"
LOG_DIR="$APP_DIR/log"
LOG_FILE="$LOG_DIR/$FNAME.log"
NODE_EXEC="/opt/local/nodejs-0.12.0/bin/node"
###############
# REDHAT chkconfig header
# chkconfig: - 58 74
# description: node-app is the script for starting a node app on boot.
### BEGIN INIT INFO
# Provides: stream
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop node
# Description: Node process for app
### END INIT INFO
###############
USAGE="Usage: $0 {start|stop|restart|status} [--force]"
FORCE_OP=true
pid_file_exists() {
[ -f "$PID_FILE" ]
}
get_pid() {
echo "$(cat "$PID_FILE")"
}
is_running() {
PID=$(get_pid)
! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
}
start_it() {
mkdir -p "$PID_DIR"
mkdir -p "$LOG_DIR"
echo "Starting $NODE_APP $PORT ..."
$NODE_EXEC "$APP_DIR/$NODE_APP" $PORT 1>"$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "Node app started with pid $!"
}
stop_process() {
PID=$(get_pid)
echo "Killing process $PID"
kill $PID
}
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}
start_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Node app already running with pid $PID"
exit 1
else
echo "Node app stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing start anyways"
remove_pid_file
start_it
fi
fi
else
start_it
fi
}
stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping node app ..."
stop_process
remove_pid_file
echo "Node app stopped"
else
echo "Node app already stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing stop anyways ..."
remove_pid_file
echo "Node app stopped"
else
exit 1
fi
fi
else
echo "Node app already stopped, pid file does not exist"
exit 1
fi
}
status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Node app running with pid $PID"
else
echo "Node app stopped, but pid file exists"
fi
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