-
-
Save toniher/f7af496a69ddb22f99fd to your computer and use it in GitHub Desktop.
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 chpst | |
### END INIT INFO | |
# ENVIROMENT VARIABLES | |
NODE_ENV="production" | |
# SCRIPT VARIABLES | |
filename=$(basename "$0") | |
extension="${filename##*.}" | |
NAME="${filename%.*}" | |
SOURCE_DIR="YOU_APP_DIR" | |
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.log" | |
OUT_LOG="$LOG_DIR/$NAME.log" | |
user="pi" | |
pidfile="/var/run/$NAME.pid" | |
nvm_script="/home/$user/.nvm/nvm.sh" | |
source "$nvm_script" # Source NVM | |
nvm_version=`nvm current` | |
forever_dir="/var/run/forever" | |
forever="/home/$user/.nvm/$nvm_version/bin/forever" | |
sed="sed" | |
cat="cat" | |
export PATH="$PATH" | |
export NODE_PATH="$NODE_PATH:/home/$user/.nvm/$nvm_version/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" | |
chown "$user" "$pidfile" | |
# 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 | |
chpst -u $user /bin/bash "$nvm_script" && \ | |
chpst -u $user "$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" | |
#echo "sudo -u $user \"$nvm_script\" \"$forever\" start -d -p \"$forever_dir\" --pidFile \"$pidfile\" -a \"$SOURCE_FILE\" -o \"$OUT_LOG\" -e \"$ERR_LOG\" --workingDir \"$WORKING_DIR\" --minUptime \"$MIN_UPTIME\" --spinSleepTime \"$SPIN_SLEEP_TIME\"" | |
RETVAL=$? | |
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 | |
chpst -u "$user" /bin/bash "$nvm_script" && chpst -u "$user" "$forever" stop -p "$forever_dir" "$foreverid" | |
else | |
echo "Instance is not running"; | |
fi | |
RETVAL=$? | |
} | |
status() { | |
if [[ -f "$pidfile" ]]; then | |
VAL=`("$cat" ${pidfile} | xargs ps -p) | wc -l` | |
if [[ "$VAL" == "2" ]]; then | |
INFO=`chpst -u "$user" /bin/bash "$nvm_script" && chpst -u "$user" "$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 | |
} | |
if [ -f "$pidfile" ]; then | |
read pid < "$pidfile" | |
else | |
pid="" | |
fi | |
if [ "$pid" != "" ]; then | |
# Gnarly sed usage to obtain the foreverid. | |
foreverid=`sudo -u "$user" "$nvm_script" && "$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*$/\1/p'` | |
else | |
foreverid="" | |
fi | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
installonboot) | |
installonboot | |
;; | |
removefromboot) | |
removefromboot | |
;; | |
*) | |
echo "Usage: {start|stop|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