Skip to content

Commit

Permalink
[fix] Use lsb functions for starting up a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck authored and indexzero committed Oct 9, 2011
1 parent 60d4329 commit 1bfdcdb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
5 changes: 2 additions & 3 deletions examples/initd-example
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#

# Source function library.
. /etc/init.d/functions
. /lib/lsb/init-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)
Expand Down Expand Up @@ -39,8 +39,7 @@ start() {
chown $user $pidfile

# Launch the application
daemon --user=$user \
env PORT=$PORT \
start_daemon
$forever start -p $forever_dir --pidfile $pidfile -l $logfile -a -d $INSTANCE_DIR $SOURCE_NAME
RETVAL=$?
else
Expand Down
66 changes: 49 additions & 17 deletions init.d/forever-services
Original file line number Diff line number Diff line change
@@ -1,41 +1,73 @@
#!/bin/bash
#
# forever-services Node init.d
# initd-example Node init.d
#
# chkconfig: 345 80 20
# description: Node init.d for forever service files
# description: Node init.d example
# processname: node
# pidfile: /var/run/forever-services.pid
# logfile: /var/log/forever-services.log
# pidfile: /var/run/initd-example.pid
# logfile: /var/log/initd-example.log
#

# Source function library.
. /lib/lsb/init-functions
path=$path:/user/local/bin

NAME=forever-services # Unique name for the application
SOURCE_NAME=main.js # Name os the applcation entry point script

NAME=forever-services
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever_dir=/var/local/forever # Forever root directory.

node=node
forever=forever
forever=`which forever`
awk=awk
sed=sed
echo `env`

start() {
echo "Starting $NAME: "
$forever service-start -p $forever_dir
RETVAL=0
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 -p $pidfile $forever service-start -p $forever_dir
else
echo "Instance already running"
fi
RETVAL=$?
}

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

stop() {
echo -n "Shutting down $NAME: "
$forever service-stop -p $forever_dir
RETVAL=0
echo -n "Shutting down $NAME node instance : "
if [ "$id" != "" ]; then
$forever service-stop -p $forever_dir
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 "\$4 == \"$pid]\" { gsub(/[\[\]]/, \"\", \$1); print \$1; }"
}
id=$(getForeverId)

case "$1" in
start)
Expand Down
6 changes: 4 additions & 2 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ cli['service-add'] = function (file, options) {
file: file,
options: options
};
console.error(service);
options.appendLog = true;
var filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');
fs.writeFileSync(filePath, JSON.stringify(service));
}
Expand Down Expand Up @@ -292,8 +292,10 @@ cli['service-start'] = function (file, options) {
var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
var service = JSON.parse(fs.readFileSync(serviceFilePath));
var file = service.file;
forever.log.info('Starting service for ' + file);
var options = service.options;
cli.start(file, options, true);
options.minUptime = 200;
cli.start(file, options, false);
});
}
cli['service-stop'] = function (file, options) {
Expand Down

0 comments on commit 1bfdcdb

Please sign in to comment.