-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |