dongraobei6719 2018-12-13 13:18
浏览 273
已采纳

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

I have one PHP file like below which I run in command prompt using below command

php myfile.php

and its working fine. I want keep running it 24/7 and its running but sometime if there any error come, its stop working so I want reload file in same command prompt. I am using it in centos, and I want see result in command prompt so Cron Job is not useful for me. I want put code which can stop file running and start it again.

<?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 
";
}

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

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

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

I can clear output result from command using below code in php

system('clear');

let me know there similar code which can reload my current file. Thanks

  • 写回答

2条回答 默认 最新

  • douyue5856 2018-12-13 13:52
    关注

    I've done this on a number of occasions to keep a PHP script running, and ensure it keeps re-running when it exits. More importantly is be able to slow things down if they start to run-away with uncaught errors however. In PHP 7+, catching the most base Exception (a \Throwable ensures that all exceptions that could be thrown are caught).

    #!/bin/bash
    
    nice php -q -f ./myfile.php -- $@
    exec $0 $@
    

    Whatever parameters are passed to the shell script, are passed into the PHP script, and when the PHP file exits, then the shell script re-runs itself (not recursively, it replaces itself) to run again. Within the PHP script you can loop as much as you like, but since restarting it is so simple and fast, I might do 50-100 loops before deliberately exiting to clean anything up.

    I further extend that simple 3-line script with some options that I can communicate out with 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 $@
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?