Re: daemon without pcntl_fork
| From: | Jim Lucas | Date: | Wed, 19 Aug 2009 07:52:31 +0000 |
| Subject: | Re: daemon without pcntl_fork | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296952@lists.php.net to get a copy of this message | ||
Lars Torben Wilson wrote:
2009/8/18 Per Jessen <per@computer.org>:Thanks for all the input you two! Currently, I have two different uses for this startup script and daemon/bg process. One will deal with a master server for one of my games and the second will be for running a PBX SMDR/CDR capturing tool. The first must be able to deal with multiple simultaneous connections where as the second simply need to listen on the local serial port, connect to a remote IP:PORT, or attach itself to a IP:PORT and wait for input from another machine to be sent to it. Once it receives the data, it simply parses it, sanitizes it, and stores it. BUT ANYWAYS... My system is OpenBSD 4.3 with PHP 5.2.8 Here is what I have come up with so far. Looks to satisfying my needs: tms_daemon #! /bin/sh # # tms_daemon #Jim Lucas wrote:Again, that's not a daemon. If it is sufficient to run a background process then that's fine, but that doesn't make it a daemon (although it shares some things in common with a daemon). The background process still has a controlling terminal (even if input and output have been redirected), and a fully-implemented daemon will typically perform other tasks which make it a good system citizen, such as: becoming session leader, chroot'ing to / so that the filesystem it was started from can be unmounted if necessary, and so on. The difference may or may not be important, depending on the task at hand. However, if an admin thinks he's got a daemon on his hands he may wonder why things behave weird if what he really has is just something which fakes it with & and nohup, since none of the important daemon housekeeping has been dealt with. TorbenDoes anybody know how to use PHP as a daemon without the use of pcntl_fork.Sure. Just start it and leave it running.I want to launch a daemon out of the /etc/rc.local when the system starts.Yep, I do that all the time.Anybody have any idea on how to do this?On an openSUSE system I would just use 'startproc', but elsewhere you could use setsid or nohup. I.e. create your CLI script with a hash-bang, then start it nohup <script> 2>&1 1>/dev/null & /Per
# Starts a listening daemon that acts as a Tribes Master Server# # Author: Jim Lucas <jlucas@cmsws.com> # # Version: 0.0.1 # set -e DESC="Tribes Master Server Daemon" DAEMON=/path/to/file/tms-0.1.5.php PIDFILE=/var/run/tms.pid SCRIPTNAME=/path/to/file/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
;;
restart|force-reload)
d_stop
sleep 0.5
d_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
# END OF SCRIPT
Well, I realize this probably doesn't have much to do /directly/ with PHP, but it is a good exercise anyways.
I can start this from /etc/rc.local or from the cli and it does the same thing either way.
Comments and suggestions are welcome. Please let me know if I can improve this at all.
TIA!
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare