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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里