开发者

Catch PHP Exits in CLI via sh

Alright, I am trying to figure this problem out. I have a class that loops indefinitely until I either restart it manually or it runs out of available ram. I've written the code to be compliant with both CLI and normal web based execution. The only difference is with web-based execution the script will last about 12 hours or so until it crashes due to memory issues. When I run it in CLI it runs far longer, (On average 4-5 days before a crash due to memory)

The script is an IRC bot that is heavily customized for what I need it to do. I don't know enough of C++, ruby, python or other languages to make something that is cross platform compliant. My dev machine is Windows and my production server is Ubuntu. Right now I have the script successfully forking off and detaching from the terminal window so I can close that with out ending the script.

But what I am trying to figure out is how to catch errors and restart the script automatically since it tends to fail at random times and not always when I am at the IRC channel to catch the failure. One last positive would be a way to catch if I req开发者_如何学Gouested a restart from the channel and have the bot restart as I am constantly adding in new code functions or just general bug fixes.

Here is my CLI start php script

#!/usr/bin/php
<?php

include_once ("./config/base_conf.php");
include_once ("./libs/irc_base.php");

if ($config ['database'] == true) {
    include_once ("./config/db_conf.php");
}
$server = getopt ( 's', array ("server::" ) );

if (! $server) {
    $SER = 'default_server';
} elseif ($server ['server'] == 'raelgun') {
    $SER = 'server_a';
} else {
    $SER = 'default_server';
}

declare ( ticks = 1 )
    ;
$pid = pcntl_fork ();
if ($pid == - 1) {
    die ( "could not fork" );
} else if ($pid) {
    exit (); // we are the parent
} else {
    // we are the child
}
// detatch from the controlling terminal
if (posix_setsid () == - 1) {
    die ( "could not detach from terminal" );
}
$posid = posix_getpid ();
$PID_FILE = "/var/run/bot_process_".$SER.".pid";
$fp = fopen ($PID_FILE , "w" ) or die("File Exists Process Running");
fwrite ( $fp, $posid );
fclose ( $fp );
// setup signal handlers
pcntl_signal ( SIGTERM, "sig_handler" );
pcntl_signal ( SIGHUP, "sig_handler" );
// loop forever performing tasks
$bot = new IRC_BOT ( $config, $SER );
function sig_handler($signo) {
    switch ($signo) {
        case SIGTERM :
            $bot->machineKill();
            unlink($PID_FILE);
            exit ();
            break;
        case SIGHUP :
            $bot->machineKill();
            unlink($PID_FILE);
            break;
        default :

        // handle all other signals
    }
}

Depending on the server I connect to since it connects to a maximum of 2 servers I run the following in the terminal to get the script running

php bot_start_shell.php --server="servernamehere" > /dev/null

So what I am trying to do is get a shell file coded correctly to monitor that script, and if it exits due to error or requested restart to restart the script.


I've used this technique for a while, where a shell script runs a PHP script, monitors the exit value and restarts.

Here's a test script that uses exit() to return a value to the shell script - 95,96 & 100 are taken as other 'unplanned restarts', handled at the bottom of the script.

#!/usr/bin/php
<?php
// cli-script.php
// for testing of the BASH script
exit (rand(95, 100));

/* normally we would return one of
# 97  - planned pause/restart
# 98  - planned restart
# 99  - planned stop, exit.
# anything else is an unplanned restart
*/

I prefer to wait a few seconds before I restart the script, to avoid wasting CPU if the script being called instantly fails, and so would be immediately restarted.

#!/bin/bash

# runPHP-Worker.sh

# a shell script that keeps looping until an exit code is given
# if its does an exit(0), restart after a second - or if it's a declared error
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, we can exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script

nice php -q -f ./cli-script.php -- $@
ERR=$?

## Possibilities
# 97    - planned pause/restart
# 98    - planned restart
# 99    - planned stop, exit.
# 0     - unplanned restart (as returned by "exit;")
#        - Anything else is also unplanned paused/restart

if [ $ERR -eq 97 ]
then
   # a planned pause, then restart
   echo "97: PLANNED_PAUSE - wait 1";
   sleep 1;
   exec $0 $@;
fi

if [ $ERR -eq 98 ]
then
   # a planned restart - instantly
   echo "98: PLANNED_RESTART, no pause";
   exec $0 $@;
fi

if [ $ERR -eq 99 ]
then
   # planned complete exit
   echo "99: PLANNED_SHUTDOWN";
   exit 0;
fi

# unplanned exit, pause, and then restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 1 sec"
sleep 1

exec $0 $@

If you don't want to do different things for each value, it really just comes down to

#!/bin/bash
php -q -f ./cli-script.php -- $@
exec $0 $@;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜