在命令行PHP-CLI中重新加载PHP文件

时间:2018-12-13 13:18:22

标签: php command-line-interface

我有一个PHP文件,如下所示,我使用以下命令在命令提示符下运行

php myfile.php

及其正常工作。我想继续运行它24/7并继续运行,但是有时如果出现任何错误,它将停止工作,因此我想在同一命令提示符下重新加载文件。我在centos中使用它,并且我想在命令提示符下看到结果,所以Cron Job对我没有用。我想要放置可以停止文件运行并重新启动的代码。

<?php

ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('src/whats.class.php');
$starttime = time();
global $w;
connectwa();


function onGetMessage($object)
{
    global $w;
    $message = $object->getMessage();
    $contact = explode("@", $object->getFrom()) [0];
    $type = "You have sent *Simple Text* Message.";
    sendMessage($contact, $type, $message);
}


function connectwa() {
    global $w;
    $wait = mt_rand(10, 15);
    global $waittime;
    $waittime = $wait*60;
    echo $waittime;
    $log = false;
    $debug = false;
    $nickname = 'Number1';
    $username = 'Your Number will be Here';
    $password = 'Your Password will be here';
    $w = new Whats($username, $nickname, $debug, $log);
    $w->eventManager()->bind('onGetMessage', 'onGetMessage');

    try {
        $w->connect();
    }
    catch(Exception $e) {
        echo 'Connection error: ' . $e->getMessage();
        exit(0);
    }

    try {
        $w->loginWithPassword($password);
    }
    catch(Exception $e) {
        echo 'Login error: ' . $e->getMessage();
        exit(0);
    }
    echo "connected \n";
}

while (1) {
    try {
        $w->pollMessage();

        if (time() - $starttime > $waittime) {
            echo "Disconnecting\n";
            $starttime = time();
            $w->disconnect();
        }
    }
    catch(Exception $e) {
    }

    if (!$w->isConnected()) {
        echo "disconnected\n";
        system('clear');
        connectwa();
    }
}
?>

我可以使用下面的php代码清除命令的输出结果

system('clear');

让我知道那里类似的代码可以重新加载当前文件。 谢谢

2 个答案:

答案 0 :(得分:0)

在while循环中使用try / catch块。这样,脚本不会因错误或异常而中断。您也可以使用catch块来记录错误。该脚本将永远继续运行。

注意

如果这是共享的主机/服务器,则应用时间限制可能不会产生任何影响。脚本将在设定的时间后中断。但我希望您在个人/专用服务器上执行此操作。

答案 1 :(得分:0)

我已经多次这样做,以保持PHP脚本运行,并确保退出后继续运行。更重要的是,如果它们因未捕获的错误而开始失控,则能够放慢速度。在PHP 7+中,捕获最基本的异常(\Throwable确保捕获了所有可能引发的异常)。

#!/bin/bash

nice php -q -f ./myfile.php -- $@
exec $0 $@

无论将什么参数传递给shell脚本,传递给PHP脚本,并且当PHP文件退出时,shell脚本都会重新运行自身(而不是递归地替换其自身)以再次运行。在PHP脚本中,您可以随意循环,但是由于重新启动是如此简单和快速,因此我可能会执行50-100次循环,然后故意退出以清理所有内容。

我进一步扩展了简单的三行脚本,并提供了一些可以与exit $n;进行交流的选项:

#!/bin/bash

# a shell script that keeps looping until an exit code is given
# if it 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, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script

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

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

if [ $ERR -eq 97 ]
then
   # a planned pause, then restart
   echo "97: PLANNED_PAUSE - wait 2";
   sleep 2;
   exec $0 $@;
fi
# ... other choices to potentially pause, or exit the shell script

# unplanned exit, pause, and restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 5 secs"
sleep 5

# rerun this shell script, replacing itself
exec $0 $@