Created
August 27, 2014 23:13
-
-
Save kwizzn/e09594f446f6a7341765 to your computer and use it in GitHub Desktop.
Daemon init script for a Node.js app using PM2 that runs under a different user
This file contains hidden or 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 | |
NAME=myApp | |
APP=/path/to/app/index.js | |
PARAMS="--foo bar" | |
PM2=/usr/local/bin/pm2 | |
USER=nobody | |
INSTANCES=max | |
LOGFILE=/tmp/.pm2/.log | |
ERRORFILE=/tmp/.pm2/.error | |
PIDFILE=/tmp/.pm2/.pid | |
export PATH=$PATH:/usr/bin | |
super() { | |
su - $USER -c "PATH=$PATH; $*" | |
} | |
start() { | |
echo "Starting $NAME" | |
super $PM2 start $APP -n $NAME -i $INSTANCES -o $LOGFILE -e $ERRORFILE -p $PIDFILE -- -a $PARAMS | |
} | |
stop() { | |
super $PM2 delete $NAME | |
} | |
restart() { | |
echo "Restarting $NAME" | |
stop | |
start | |
} | |
reload() { | |
echo "Reloading $NAME" | |
super $PM2 reload $NAME | |
} | |
status() { | |
echo "Status for $NAME:" | |
super $PM2 list | |
RETVAL=$? | |
} | |
kill() { | |
echo "Killing $NAME" | |
super $PM2 kill | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
restart) | |
restart | |
;; | |
reload) | |
reload | |
;; | |
force-reload) | |
reload | |
;; | |
kill) | |
kill | |
;; | |
*) | |
echo "Usage: {start|stop|status|restart|reload|force-reload|kill}" | |
exit 1 | |
;; | |
esac | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment