-
-
Save MateusZitelli/06f72fb448524acc3f7c to your computer and use it in GitHub Desktop.
Init.d script for Node.js/IO.js servers, with forever and nvm.
This file contains 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/bash | |
### BEGIN INIT INFO | |
# Provides: YOUR NAME HERE | |
# Required-Start: $local_fs $remote_fs $network $syslog $named | |
# Required-Stop: $local_fs $remote_fs $network $syslog $named | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start/stop the forever nodejs application. Requires sudo | |
### END INIT INFO | |
. /lib/lsb/init-functions | |
# ENVIROMENT VARIABLES | |
NODE_ENV="production" | |
# SCRIPT VARIABLES | |
filename=$(basename "$0") | |
extension="${filename##*.}" | |
NAME="${filename%.*}" | |
SOURCE_DIR="SOURCE_FOLDER" | |
SOURCE_FILE="$SOURCE_DIR/index.js" | |
PREPARE_STOP_SCRIPT="prepareForStop.js" | |
LOG_DIR="/var/log" | |
# FOREVER VARIABLES | |
MIN_UPTIME=1000 # Minimum uptime (millis) for a script to not be considered "spinning" | |
SPIN_SLEEP_TIME=5000 # Time to wait (millis) between launches of a spinning script. | |
WORKING_DIR="$SOURCE_DIR" | |
ERR_LOG="$LOG_DIR/$NAME-err.log" | |
OUT_LOG="$LOG_DIR/$NAME-out.log" | |
user="USER_NAME" | |
pidfile="/var/run/$NAME.pid" | |
nvm_script="/home/$user/.nvm/nvm.sh" | |
. $nvm_script | |
nvm_version_path="PATH OF NODE/IO.JS VERSION" #E.g.: "versions/io.js/v2.0.0" or using node "v0.10.38" | |
forever_dir="/var/run/forever" | |
forever="/home/$user/.nvm/$nvm_version_path/bin/forever" | |
iojs="/home/$user/.nvm/$nvm_version_path/bin/iojs" // In case of node use "/home/$user/.nvm/$nvm_version_path/bin/node" | |
sed="sed" | |
cat="cat" | |
awk="awk" | |
export PATH="$PATH" | |
export NODE_PATH="$NODE_PATH:/home/$user/.nvm/$nvm_version_path/lib/node_modules" | |
start() { | |
echo "Starting $NAME node instance: " | |
if [ "$foreverid" == "" ]; then | |
# Create the pid file, making sure that | |
# the target use has access to them | |
touch "$pidfile" | |
touch "$ERR_LOG" | |
touch "$OUT_LOG" | |
chown "$user" "$pidfile" | |
chown "$user" "$ERR_LOG" | |
chown "$user" "$OUT_LOG" | |
# Ensure the log dir exists | |
if [ ! -d "$LOG_DIR" ] | |
then | |
mkdir -p "$LOG_DIR" | |
fi | |
# Ensure the forever dir misc file dir exists | |
if [ ! -d "$forever_dir" ] | |
then | |
mkdir -p "$forever_dir" | |
chown "$user" "$forever_dir" | |
fi | |
# Launch the application | |
pushd "$SOURCE_DIR" > /dev/null | |
sudo -u $user "$iojs" "$forever" start -d -p "$forever_dir" --pidFile "$pidfile" \ | |
-o "$OUT_LOG" -e "$ERR_LOG" --workingDir "$WORKING_DIR" \ | |
--minUptime $MIN_UPTIME --spinSleepTime $SPIN_SLEEP_TIME -a "$SOURCE_FILE" | |
RETVAL=$? | |
popd | |
else | |
echo "Instance already running" | |
RETVAL=0 | |
fi | |
} | |
stop() { | |
echo -n "Shutting down $NAME node instance : " | |
if [ ! "$foreverid" == "" ]; then | |
if [ -f "$PREPARE_STOP_SCRIPT" ] | |
then | |
sudo -u "$user" "$nvm_script" && "$SOURCE_DIR/PREPARE_STOP_SCRIPT" | |
fi | |
sudo -u "$user" "$iojs" "$forever" stop -p "$forever_dir" "$foreverid" | |
else | |
echo "Instance is not running"; | |
fi | |
foreverid=""; | |
RETVAL=$? | |
} | |
status() { | |
if [[ -f "$pidfile" ]]; then | |
VAL=`("$cat" ${pidfile} | xargs ps -p) | wc -l` | |
if [[ "$VAL" == "2" ]]; then | |
INFO=`sudo -u "$user" "$iojs" "$forever" list --plain | $sed -n 's/^data:\s\+\[\([0-9]\+\)\]\s\+.*\s\+'$pid'\s\+\S\+\s\+\([0-9]\+:[0-9]\+:[0-9]\+:[0-9]\+\.[0-9]\+\)\s*$/Fid \1 Uptime \2/p'` | |
echo "Running ($INFO)" | |
RETVAL=0 | |
else | |
echo "Not running" | |
RETVAL=-1 | |
fi | |
else | |
echo "Not running" | |
RETVAL=-1 | |
fi | |
} | |
installonboot() { | |
if [[ `ls -l /etc/rc?.d/*$NAME 2>/dev/null | wc -l` == 0 ]]; then | |
update-rc.d "$NAME" defaults | |
else | |
echo "Already Installed on boot" | |
RETVAL=-1 | |
fi | |
} | |
removefromboot() { | |
if [[ `ls -l /etc/rc?.d/*$NAME 2>/dev/null | wc -l` != 0 ]]; then | |
update-rc.d -f "$NAME" remove | |
else | |
echo "Not currently installed for boot" | |
RETVAL=-1 | |
fi | |
} | |
getForeverId() { | |
local pid=$(pidofproc -p $pidfile) | |
if [ "$pid" != "" ]; then | |
foreverId=`sudo -u $user $iojs $forever list -p $forever_dir --plain | $awk "\\\$7 && \\\$7 == "$pid" {print \\\$3;}"`; | |
echo $foreverId; | |
else | |
echo ""; | |
fi | |
} | |
foreverid=$(getForeverId) | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
stop | |
start | |
;; | |
status) | |
status | |
;; | |
installonboot) | |
installonboot | |
;; | |
removefromboot) | |
removefromboot | |
;; | |
*) | |
echo "Usage: {start|stop|restart|status|installonboot|removefromboot}" | |
exit 1 | |
;; | |
esac | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment