Created
February 24, 2014 07:28
-
-
Save brennanMKE/9183375 to your computer and use it in GitHub Desktop.
Node Server Script
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/sh | |
# Based On: https://github.com/chovy/node-startup/blob/master/init.d/node-app | |
# strict | |
set -e | |
NODE_ENV="development" | |
NODE_APP='app.js' | |
APP_DIR='.'; | |
PID_DIR=$APP_DIR/pid | |
PID_FILE=$PID_DIR/app.pid | |
LOG_DIR=$APP_DIR/log | |
LOG_FILE=$LOG_DIR/app.log | |
CONFIG_DIR=$APP_DIR/config | |
PORT=3000 | |
NODE_EXEC=`which node` | |
NPM_EXEC=`which npm` | |
############### | |
start_app (){ | |
if [ -f $PID_FILE ] | |
then | |
echo "$PID_FILE exists, process is already running or crashed" | |
else | |
echo "Starting node app..." | |
if [ ! -d $PID_DIR ]; then | |
mkdir $PID_DIR | |
fi | |
if [ ! -d $LOG_DIR ]; then | |
mkdir $LOG_DIR | |
fi | |
if [ ! -d node_modules ]; then | |
$NPM_EXEC install | |
fi | |
PORT=$PORT NODE_ENV=$NODE_ENV NODE_CONFIG_DIR=$CONFIG_DIR $NODE_EXEC $APP_DIR/$NODE_APP 1>$LOG_FILE 2>&1 & | |
echo $! > $PID_FILE; | |
fi | |
} | |
stop_app (){ | |
if [ ! -f $PID_FILE ] | |
then | |
echo "$PID_FILE does not exist, process is not running" | |
else | |
echo "Stopping $APP_DIR/$NODE_APP ..." | |
echo "Killing `cat $PID_FILE`" | |
kill `cat $PID_FILE`; | |
rm -f $PID_FILE; | |
echo "Node stopped" | |
fi | |
} | |
case "$1" in | |
start) | |
start_app | |
;; | |
stop) | |
stop_app | |
;; | |
restart) | |
stop_app | |
start_app | |
;; | |
status) | |
if [ -f $PID_FILE ] | |
then | |
PID=`cat $PID_FILE` | |
if [ -z "`ps -ef | grep $PID | grep -v grep`" ] | |
then | |
echo "Node app stopped but pid file exists" | |
else | |
echo "Node app running with pid $PID" | |
fi | |
else | |
echo "Node app stopped" | |
fi | |
;; | |
*) | |
echo "Usage: server.sh {start|stop|restart|status}" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment