-
-
Save sumpton/5778486 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 | |
# | |
# chkconfig: - 55 45 | |
# init.d example for Forever & Node | |
# | |
# modeled after | |
# https://gist.github.com/jinze/3748766 | |
# | |
# supports vhost | |
# | |
# pidfile: /var/run/nodejs/initd-example.pid | |
# logfile: /var/log/nodejs/initd-example.log | |
# Source function library. | |
. /etc/init.d/functions | |
NAME=feeds-process # Unique name for the application | |
NODE_ENV=production # Node environment | |
PORT=3000 # Port (in this case the application uses process.env.PORT to set the port) | |
SOURCE_NAME=app.js # Name os the applcation entry point script | |
USER=nodejs | |
INSTANCE_DIR=/var/www/$NAME # Location of the application source | |
COMMAND=node # Command to run | |
PID_DIR=/var/run/nodejs | |
PIDFILE=$PID_DIR/$NAME.pid | |
LOG_DIR=/var/log/nodejs | |
LOGFILE=$LOG_DIR/$NAME.log | |
FOREVER_DIR=/var/run/forever # Forever root directory. | |
NODE=node | |
FOREVER=/usr/bin/forever | |
AWK=awk | |
SED=sed | |
start() { | |
echo "Starting $NAME node instance: " | |
if [ "$FOREVER_ID" = "" ]; then | |
# Create the log and pid files, making sure that the target use has access to them | |
mkdir -p $PID_DIR | |
chown $USER $PID_DIR | |
mkdir -p $LOG_DIR | |
chown $USER $LOG_DIR | |
touch $LOGFILE | |
chown $USER $LOGFILE | |
touch $PIDFILE | |
chown $USER $PIDFILE | |
# Launch the application | |
daemon $FOREVER start -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -a --sourceDir $INSTANCE_DIR -c $COMMAND $SOURCE_NAME | |
RETVAL=$? | |
else | |
echo "Instance already running" | |
RETVAL=0 | |
fi | |
} | |
restart() { | |
echo -n "Restarting $NAME node instance : " | |
if [ "$FOREVER_ID" != "" ]; then | |
$FOREVER restart -p $FOREVER_DIR $FOREVER_ID | |
RETVAL=$? | |
else | |
start | |
fi | |
} | |
stop() { | |
echo -n "Shutting down $NAME node instance : " | |
if [ "$FOREVER_ID" != "" ]; then | |
$FOREVER stop -p $FOREVER_DIR $FOREVER_ID | |
else | |
echo "Instance is not running"; | |
fi | |
RETVAL=$? | |
} | |
getForeverId() { | |
# local PID=$(pidofproc -p $PIDFILE) | |
read PID < $PIDFILE | |
#$FOREVER list -p $FOREVER_DIR | $SED -e 's/\x1b\[[0-9; ]*m//g' | $AWK "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }"; | |
$FOREVER list | grep $PID | $AWK "\$7 && \$7 == \"$PID\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }" | |
} | |
if [ -f $PIDFILE ]; then | |
FOREVER_ID=$(getForeverId) | |
#echo ID: $FOREVER_ID | |
else | |
FOREVER_ID='' | |
fi | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status -p ${PIDFILE} | |
;; | |
restart) | |
restart | |
;; | |
*) | |
echo "Usage: {start|stop|status|restart}" | |
exit 1 | |
;; | |
esac | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment