PHP daemon
First off, I need to say upfront, that this is not really a daemon at all. It looks like a daemon. It starts like a daemon. But it is missing many of the common daemon things...
Anyways, on to what I have created.
Lets start off by simply showing you what I have working.
#! /bin/sh
#
# tms_daemon
#
# Starts a listening daemon that acts as a Tribes Master Server
#
# Author: Jim Lucas <jlucas@cmsws.com>
#
# Version: 0.0.1
#
# Exit immediately if a command exits with a nonzero exit status.
set -e
DESC="Tribes Master Server Daemon"
DAEMON=/var/www/domains/tribesmasterserver.com/scripts/tms-0.1.5.php
PIDFILE=/var/run/tms.pid
SCRIPTNAME=/var/www/domains/tribesmasterserver.com/scripts/tms_daemon
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
#
# Function that starts the daemon/service.
#
d_start() {
if [ -f $PIDFILE ]; then
echo "$DESC already running: PID# `cat $PIDFILE`"
exit 1
else
echo -n "Starting $DESC"
nohup $DAEMON 2>&1 1>/dev/null &
sleep 0.5
ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}' > $PIDFILE
echo ". [`cat $PIDFILE`]"
fi
}
#
# Function that stops the daemon/service.
#
d_stop() {
if [ -f $PIDFILE ]; then
echo -n "Stopping $DESC"
kill `cat $PIDFILE`
rm $PIDFILE
echo "."
else
echo "$DESC is not running"
exit 1
fi
}
case "$1" in
start)
d_start
;;
stop)
d_stop
;;
status)
if [ -f $PIDFILE ]; then
echo "$DESC is running: PID# `cat $PIDFILE`"
else
echo "$DESC is not running"
fi
;;
# Meant to give you a way to "cleanup" the possible remains of a dead process
cleanup)
PID="`ps aux | grep $DAEMON | grep -v grep | awk -F' ' '{print $2}'`"
kill $PID
rm $PIDFILE
;;
restart|force-reload)
d_stop
sleep 0.5
d_start
;;
# Default action
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
Basically, the above will start the $DAEMON script using the *nix command nohup (see references below). It then redirects STDOUT and STDERR to /dev/null. Then I use "&" to place the process into the background.
References
nohup
http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html∞
http://www.idevelopment.info/data/Unix/General_UNIX/GENERAL_RunningUNIXCommandsImmunetoHangups_nohup.shtml∞
There are no comments on this page. [Add comment]