|
Posted by Sabine Dinis Blochberger on 04/09/07 12:04
I have written a rather simple php script using class_jabber.php
(http://www.centova.com). It just logs in as a bot, and keeps track of
the on/offline status of our support staff (writing to a firebird2
table). It works fine as is when it's running.
The problem is that the daemon will stop working (exits memory) without
any trace in the log files after seemingly random amounts of time.
I use set_time_limit(0); to prevent PHP stopping the script. I have put
up signal handlers (I know that some signals aren't supposed to be
catchable).
Here's some code from the php script:
function sig_handler($signo) {
switch ($signo) {
case SIGTERM:
case SIGSTOP:
case SIGKILL:
case SIGINT:
// handle shutdown tasks
error_log(date('\[d-m-Y H:i:s\] ') . "Received TERM signal.\r\n",
3, $logfile);
$jab->terminated = true;
// disconnect from the Jabber server
$jab->disconnect();
// update database, all unavailable when bot exits
setAllUnavailable();
return;
break;
case SIGHUP:
// handle restart tasks
break;
default:
// handle all other signals
}
}
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGSTOP, "sig_handler");
pcntl_signal(SIGKILL, "sig_handler");
pcntl_signal(SIGINT, "sig_handler");
define("CBK_FREQ", 1); // fire a callback event every second
// create an instance of the Jabber class
$jab = new Jabber(false);
// create an instance of our event handler class
$test = new TestMessenger($jab);
// set handlers for the events we wish to be notified about
$jab->set_handler("connected", $test, "handleConnected");
$jab->set_handler("authenticated", $test, "handleAuthenticated");
$jab->set_handler("authfailure", $test, "handleAuthFailure");
$jab->set_handler("heartbeat", $test, "handleHeartbeat");
$jab->set_handler("error", $test, "handleError");
$jab->set_handler("message_normal", $test, "handleMessage");
$jab->set_handler("message_chat", $test, "handleMessage");
// connect to the Jabber server
if (!$jab->connect(JABBER_SERVER)) {
die("Could not connect to the Jabber server!\n");
}
// now, tell the Jabber class to begin its execution loop
$jab->execute(CBK_FREQ);
// Note that we will not reach this point (and the execute() method
// will not
// return) until $jab->terminated is set to TRUE. The execute()
// method simply
// loops, processing data from (and to) the Jabber server, and
// firing events
// (which are handled by our TestMessenger class) until we tell it
// to terminate.
//
// This event-based model will be familiar to programmers who have
// worked on
// desktop applications, particularly in Win32 environments.
// disconnect from the Jabber server
$jab->disconnect();
// update database, all unavailable when bot exits
setAllUnavailable();
error_log(date('\[d-m-Y H:i:s\] ') . "Stopped.\r\n", 3, $logfile);
ibase_close($connection);
Here's the daemon script for running it in Debian (etch).
#! /bin/sh
#
# supportbotd initscript
# script for a php-based jabber control bot
#
# Author: Sabine Dinis Blochberger, Op3racional.
#
#
set -e
PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/loc\
al/imbot
DESC="IM support bot"
NAME=op3_supportbot.php
DAEMON=/usr/local/imbot/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/usr/local/imbot/supportbotd
# Gracefully exit if the package has been removed.
#test -x $DAEMON || exit 0
# Read config file if it is present.
#if [ -r /etc/default/$NAME ]
#then
# . /etc/default/$NAME
#fi
#
# Function that starts the daemon/service.
#
d_start() {
start-stop-daemon --start --quiet --background \
--make-pidfile --pidfile $PIDFILE \
--exec /usr/bin/php $DAEMON &
}
#
# Function that stops the daemon/service.
#
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--signal SIGTERM
}
#
# Function that sends a SIGHUP to the daemon/service.
#
d_reload() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--name $NAME --signal 1
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
#reload)
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
# If the daemon responds to changes in its config file
# directly anyway, make this an "exit 0".
#
# echo -n "Reloading $DESC configuration..."
# d_reload
# echo "done."
#;;
restart|force-reload)
#
# If the "reload" option is implemented, move the
"force-reload"
# option to the "reload" entry above. If not,
"force-reload" is
# just the same as "restart".
#
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
# echo "Usage: $SCRIPTNAME
{start|stop|restart|reload|force-reload}" >&\
2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
Note that stopping via this bash script doesn't work. I have to kill -9
the process. Any help on either these codes is appreciated. Thanks :)
PHP version is 5.
[Back to original message]
|