Skip to content

Instantly share code, notes, and snippets.

@vbtechsupport
Last active August 16, 2016 08:28
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vbtechsupport/7117636 to your computer and use it in GitHub Desktop.
ghost.centminmod.com CentOS 6.4 init.d startup script configured for per Ghost install instance + clearing Nginx proxy_cache on restart. http://ghost.centminmod.com/ghost-blog-node-js-init-startup-script/
#!/bin/bash
###########################################
# chkconfig: 2345 98 02
#
# description: PM2 next gen process manager for Node.js
# processname: ghost1
#
### BEGIN INIT INFO
# Provides: ghost1
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
###########################################
if [ -f /proc/user_beancounters ]; then
ulimit -s 256
fi
###########################################
PM2=/usr/local/lib/node_modules/pm2/bin/pm2
NODE=/usr/local/bin/node
USER=root
export NODE_ENV=production
###########################################
# edit NAME to unique name for each specific Ghost instance
# i.e. ghost1 ghost2 etc
# edit APP_DIR to each specific Ghost install directory
NAME=ghost1
APP_DIR=/home/nginx/domains/ghost.centminmod.com/ghost
APP_START=index.js
APP_NAME=$NAME
PID=$(su -l $USER -c "$NODE $PM2 list" | grep "$APP_NAME" | awk '{print $4}')
PROXYCACHE=y
PROXYCACHEDIR='/home/nginx/domains/ghost.centminmod.com/ghostcache'
###########################################
ngxcache() {
if [[ "$PROXYCACHE" = [yY] ]]; then
# clear proxy_cache
# i.e. nginx.conf
# proxy_cache_path /home/nginx/domains/ghost.centminmod.com/ghostcache levels=1:2 keys_zone=ghostcache:80m
# max_size=100m inactive=60m;
rm -rf ${PROXYCACHEDIR}/*
service nginx restart
fi
}
super() {
su -l $USER -c "$1 $2 $3 $4 $5 $6 $7 $8"
}
start() {
echo "Starting $NAME"
cd $APP_DIR
super NODE_ENV=production $NODE $PM2 start ${APP_DIR}/${APP_START} -x --name $APP_NAME
}
stop() {
echo "Shutting down $NAME"
cd $APP_DIR
super NODE_ENV=production $NODE $PM2 stop ${APP_DIR}/${APP_START} -x --name $APP_NAME
}
restart() {
echo "Restarting $NAME"
stop
start
ngxcache
}
status() {
echo "Status for $NAME:"
cd $APP_DIR
super $NODE $PM2 list
RETVAL=$?
}
###########################################
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
@kenetik
Copy link

kenetik commented Nov 11, 2013

Using the current code I was given an error when trying to stop or restart the service. Please see:

[root@myserver ghost1]# service ghost1 restart
Restarting ghost1
Shutting down ghost1

  error: missing required argument `pm2_id|name|all'

Starting ghost1
PM2 [ERROR] Script already launched, add -f option to force re execution
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@myserver ghost1]# service ghost1 stop
Shutting down ghost1

  error: missing required argument `pm2_id|name|all'

I altered the stop function as defined by stop() { as such: I changed super $NODE $PM2 stop $PID to super NODE_ENV=production $NODE $PM2 stop ${APP_DIR}/${APP_START} -x --name $APP_NAME and now service ghost1 stop and service ghost1 restart both work for me as expected.

Did I do something wrong initially?

@centminmod
Copy link

thanks for feedback, i'm still new to node.js, Ghost and pm2 so I could be wrong myself. Will test your changes and see how it goes.

it could be the PID variable

 PID=$(su -l $USER -c "$NODE $PM2 list" | grep "$APP_NAME" | awk '{print $4}')

strange i tried

super NODE_ENV=production $NODE $PM2 stop ${APP_DIR}/${APP_START} -x --name $APP_NAME

but get error

service ghost stop  
Shutting down ghost1

  error: option `-n --name <name>' argument missing

@centminmod
Copy link

Okay i see the problem.. will update the init.d script.. !

my copy of init.d had missing 8th variable option

su -l $USER -c "$1 $2 $3 $4 $5 $6 $7 $8"

@kenetik
Copy link

kenetik commented Nov 16, 2013

Thanks for the Update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment