Skip to content

Instantly share code, notes, and snippets.

@jinze
Created September 19, 2012 09:50
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jinze/3748766 to your computer and use it in GitHub Desktop.
Save jinze/3748766 to your computer and use it in GitHub Desktop.
forever init.d example
#!/bin/bash
#
# initd-example Node init.d
#
# chkconfig: 345 80 20
# description: Node init.d example
# processname: node
# pidfile: /var/run/initd-example.pid
# logfile: /var/log/initd-example.log
#
# Source function library.
. /lib/lsb/init-functions
NAME=initd-example # Unique name for the application
NODE_ENV=production # Node environment
PORT=1234 # Port (in this case the application uses process.env.PORT to set the port)
INSTANCE_DIR=/var/www/$NAME # Location of the application source
COMMAND=coffee # Command to run
SOURCE_NAME=app.coffee # Name os the applcation entry point script
user=apache
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever_dir=/var/run/forever # Forever root directory.
node=node
forever=forever
awk=awk
sed=sed
start() {
echo "Starting $NAME node instance: "
if [ "$id" = "" ]; then
# Create the log and pid files, making sure that the target use has access to them
touch $logfile
chown $user $logfile
touch $pidfile
chown $user $pidfile
# Launch the application
start_daemon
$forever start -p $forever_dir --pidFile $pidfile -l $logfile -a -d $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
RETVAL=$?
else
echo "Instance already running"
RETVAL=0
fi
}
restart() {
echo -n "Restarting $NAME node instance : "
if [ "$id" != "" ]; then
$forever restart -p $forever_dir $id
RETVAL=$?
else
start
fi
}
stop() {
echo -n "Shutting down $NAME node instance : "
if [ "$id" != "" ]; then
$forever stop -p $forever_dir $id
else
echo "Instance is not running";
fi
RETVAL=$?
}
getForeverId() {
local pid=$(pidofproc -p $pidfile)
$forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
}
id=$(getForeverId)
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile}
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
@hectorcorrea
Copy link

For what it worth, I had to add --sourceDir in between -d and $INSTANCE_DIR for this to work: -d --sourceDir $INSTANCE_DIR

@hectorcorrea
Copy link

Great script by the way!

@srlowe
Copy link

srlowe commented May 7, 2013

Yeah, the -d option switches on debug output, so you probably want to remove it altogether. I guess jinze thought this to be the source directory option.

@xiaods
Copy link

xiaods commented May 14, 2013

$awk "$6 && $6 == "$pid" { gsub(/[[]]/, "", $2); print $2; }";

this regex not correct grep forever id from forever list string.
0.10.8

@Talos51
Copy link

Talos51 commented Mar 27, 2015

Late comment but as xiaods said your regex to get forever id doesn't works so I must find a trick, maybe it will help someone :

getForeverId() {
    ps -eo pid,command | grep "$INSTANCE_DIR/$SOURCE_NAME" | grep -v grep | awk "{print \$1;}"
} 

Btw thanks for this script works awesome for other functions.

@hillkim7
Copy link

hillkim7 commented Feb 24, 2017

Despite of above modification, the forever Id Not Found error still exists in Ubuntu 16.04.1 LTS.
Hopefully recent forever, v0.15.3, supports PID as a command argument. No needs to find correspondent Id by PID.
Modified script:

getForeverId() {  
  local pid=$(pidofproc -p $pidfile $NAME)  
  echo $pid  
}

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