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

在命令行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 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)