APseudoUtopia wrote:
> Hey list,
>
> I have a php cli script that listens on a UDP socket and, when data is
> sent to the socket, the script inserts it into a database. I'm using
> the real BSD socket functions, not fsock.
>
> The script runs socket_create(), then socket_bind(). Then it starts a
> while(TRUE) loop. Within the loop, it runs socket_recvfrom(). I have
> it running 24/7 inside a screen window.
>
> I'm curious as to the cpu/memory/etc usage of a while(true) loop. The
> `top` command shows that the process is in the sbwait state (the OS is
> FreeBSD). I'm contemplating adding a usleep or even a sleep inside to
> loop. Would this be beneficial? I'm not too sure of how the internals
> of PHP work in terms of loops and such.
>
> Thanks.
>
Here is something I wrote a few years ago. I still have this running on
my system today...
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Set time limit to indefinite execution
set_time_limit(0);
// Set the ip and port we will listen on
define('LISTEN_IP', 'x.x.x.x'); // IP to listin on
define('LISTEN_PORT', 28000); // Port number to listen on
define('PACKET_SIZE', 512); // 512 bytes
define('SOCKET_TIMOUT', 2); // In Seconds
/* Open a server socket to port 1234 on localhost */
if ( $socket = @stream_socket_server('udp://'.LISTEN_IP.':'.LISTEN_PORT,
$errno, $errstr, STREAM_SERVER_BIND) ) {
echo "Running!!!\n";
while ( true ) {
// Output something to the screen to indicate that it is running
echo '.';
/* Get the exact same packet again, but remove it from the buffer
* this time.
*/
$buff = stream_socket_recvfrom($socket, PACKET_SIZE, 0, $remote_ip);
# Checking to see if someone is sending an invalid packet to my
# server
# Minimum packet size is 4 bytes
if ( strlen($buff) <= 4 ) {
continue;
}
print_r($buff);
}
fclose($socket);
}
echo "Done!!!\n";
###################END
Until recently, I was using screen also, but I have now switched over to
a new start/stop script. I run OpenBSD 4.3 & 4.5 on most of my boxes.
Once configured, this shell script can start/stop/status the given process.
I have been told that this does not start a /true/ daemon. But for me
it is close enough.
change the 4 variables at the top, just under the set -e line, and you
should have it.
Let me know if you have any problems.
#! /bin/ksh
#
# tms_daemon
#
# Starts a listening daemon that acts as a Tribes Master Server
#
# Author: Jim Lucas <[email protected]>
#
# Version: 0.0.1
#
set -e
DESC="Name of service"
DAEMON=/path/to/file.php
PIDFILE=/var/run/<NAME>.pid
SCRIPTNAME=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
;;
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
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
##############END
Jim Lucas
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php