Skip to content

Instantly share code, notes, and snippets.

@reywood
Last active July 27, 2017 18:05
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reywood/11184250 to your computer and use it in GitHub Desktop.
Save reywood/11184250 to your computer and use it in GitHub Desktop.
SysVinit boot script for running a bundled Meteor app under Forever as a service. Should work with CentOS, Redhat, Amazon Linux, etc.
#!/bin/bash
#
# Service script for running a bundled Meteor application under Forever.
# Meteor settings JSON file should be in /etc/meteor/[YOUR APP NAME].json,
# and the METEOR_SETTINGS var below should be updated as appropriate.
#
# chkconfig: 345 80 20
# description: My node app
#
. /etc/init.d/functions
NAME=my-app
SOURCE_DIR=/var/www/my-app
SOURCE_FILE=main.js
SOURCE_FILE_PATH=$SOURCE_DIR/$SOURCE_FILE
export PORT=80
export ROOT_URL=http://my-app.example.com
export MONGO_URL=mongodb://username:password@hostname:27017/my-db
export MONGO_OPLOG_URL=mongodb://username:password@hostname:27017/oplog
export METEOR_SETTINGS=$(cat /etc/meteor/$NAME.json)
export FOREVER_ROOT=/var/run/forever
user=nodejs
pidfile=/var/run/$NAME.pid
lockfile=/var/lock/subsys/$NAME
logfile=/var/log/nodejs/$NAME-node.log
forever=`which forever`
if [ -z "$forever" ]; then
echo "ERROR: can't find forever"
exit 1
fi
runuser="runuser -s /bin/bash $user -c"
export NODE_PATH=$NODE_PATH:/usr/lib/node_modules
start() {
echo -n "Starting $NAME node instance: "
mkdir -p /var/log/nodejs
chown $user /var/log/nodejs
mkdir -p $FOREVER_ROOT
chown $user $FOREVER_ROOT
touch $logfile
chown $user $logfile
touch $pidfile
chown $user $pidfile
$runuser "$forever start \
-l $logfile \
-a \
--pidFile $pidfile \
--minUptime 5000 \
--spinSleepTime 2000 \
$SOURCE_FILE_PATH > /dev/null 2>&1"
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
[ $RETVAL -eq 0 ] && success $"$NAME startup" || failure $"$NAME startup"
echo
}
stop() {
echo -n "Stopping $NAME node instance: "
$runuser "$forever stop $SOURCE_FILE_PATH > /dev/null 2>&1"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $lockfile
[ $RETVAL -eq 0 ] && success $"$NAME shutdown" || failure $"$NAME shutdown"
echo
}
rh_status() {
status -p $pidfile $NAME
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
rh_status_q || exit 0
stop
;;
restart)
stop
start
;;
status)
rh_status
;;
*)
echo "Usage: {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment