Skip to content

Commit

Permalink
Init.d Example script
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Feb 28, 2011
1 parent 51bc6c0 commit b181dd7
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions examples/initd-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/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.
. /etc/init.d/functions

NAME=initd-example # Unique name for the application
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
SOURCE_NAME=main.js # Name os the applcation entry point script

user=apache
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log

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
daemon --user=$user \
env PORT=$PORT \
$forever start --pidfile $pidfile -l $logfile -a -d $INSTANCE_DIR $SOURCE_NAME
RETVAL=$?
else
echo "Instance already running"
RETVAL=0
fi
}

restart() {
echo -n "Restarting $NAME node instance : "
if [ "$id" != "" ]; then
$forever restart $id
RETVAL=$?
else
start
fi
}

stop() {
echo -n "Shutting down $NAME node instance : "
if [ "$id" != "" ]; then
$forever stop $id
else
echo "Instance is not running";
fi
RETVAL=$?
}

getForeverId() {
local pid=$(pidofproc $pidfile)
$forever list | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$4 == \"$pid]\" { gsub(/[\[\]]/, \"\", \$1); print \$1; }"
}
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

0 comments on commit b181dd7

Please sign in to comment.