【问题标题】:My PHP While(true) automatically stopped in cpanel我的 PHP While(true) 在 cpanel 中自动停止
【发布时间】:2014-03-28 10:55:13
【问题描述】:

我已经在我的本地主机中测试了这个脚本一整天,它仍然可以工作。但是在 cpanel 中这个脚本会自动停止

    set_time_limit(0);
    ini_set('memory_limit', '-1');
    $run = TRUE;
    $nowtime = date("Y-m-d H:i");

    while($run){
        $stop = GetSetting("Stop");
        $time = date("Y-m-d H:i");
        if($stop){
            $run = FALSE; // stopped when setting changed
            continue;
        }
        if($time != $nowtime){
            log_message("debug","Still Running On ".$time); //checking if it still running every minute
        }
        $nowtime = $time;
    }

你认为发生了什么?有哪些可能导致它停止的情况?是关于记忆的吗?还是服务器配置?还是其他可能性?

【问题讨论】:

  • 你检查日志了吗? “getSetting”函数有什么作用(顺便说一句。函数应该以小写字母开头)?你有没有检查时间限制是否改变了?你到底想用这个脚本归档什么?
  • GetSetting 是检查数据库的函数,它的目的是在设置为 1 时停止 while,所以当 Stop == 1 时停止。我创建了一个持续运行的守护程序系统,以创建一些 cron 作业无法处理的作业。时间限制没问题,日志中没有“最大执行”错误和“内存限制”错误
  • 您没有检查 === 1 的停止。您也可以只使用“break;”相反,如果您在 if ($stop) 中做了什么。你是如何运行这个脚本的? PHPCLI?阿帕奇?对我来说,这整件事听起来更像是一个目的问题。 PHP 不是为这样的东西而设计的。你应该切换到不同的东西,我不知道,至少是一个 bash 脚本或者更接近系统的东西,比如 C 来创建这样一个守护进程。无论哪种方式:我想不出每分钟运行的 cronjob 无法处理的任何事情。

标签: php cpanel


【解决方案1】:

您可以通过从“命令行”运行 PHP 来轻松完成此操作。这是显示“概念证明”的示例脚本。我在 Windows XP 上运行 PHP 5.3.18 (oi!, stop laffin')。

我显示捕获的输出。

脚本:

<?php
    // P:\developer\xampp\htdocs\testmysql\Q22710846.php
    //
    // run this from the command line - press ctrl-c to kill the task.

    set_time_limit(0);
    $run = TRUE;
    $nowtime = date("Y-m-d H:i:s");

    $debug = false; // set true to stop after 10
    $loopCount = 10;

    if (!$debug) {
       echo "press ctrl-c to end the task as it will forever!\n\n";
    }
    while(($debug && $loopCount) || (!$debug && $run)) { // will run forever...

        $nowtime = date("Y-m-d H:i:s");
        echo 'The time now is, beep, beep, beep : ', $nowtime, "\n";

        // generate a random delay of between 1 and 10 seconds
        $delay = mt_rand(1, 10);
        echo 'waiting for : ', $delay, " seconds\n";
        sleep($delay);

        // log_message("debug","Still Running On ".$time); //checking if it still running every minute

        if ($debug) {
          $loopCount--;
        }

    }
?>

样本输出...

P:\developer\xampp\htdocs\testmysql>php Q22710846.php
press ctrl-c to end the task as it will forever!

The time now is, beep, beep, beep : 2014-04-02 11:19:03
waiting for : 8 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:11
waiting for : 7 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:18
waiting for : 6 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:24
waiting for : 6 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:30
waiting for : 5 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:35
waiting for : 8 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:43
waiting for : 8 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:51
waiting for : 2 seconds
The time now is, beep, beep, beep : 2014-04-02 11:19:53
waiting for : 8 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:01
waiting for : 1 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:02
waiting for : 3 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:05
waiting for : 7 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:12
waiting for : 9 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:21
waiting for : 9 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:30
waiting for : 8 seconds
The time now is, beep, beep, beep : 2014-04-02 11:20:38
waiting for : 1 seconds
^C
P:\developer\xampp\htdocs\testmysql>

【讨论】:

    【解决方案2】:

    这是因为您的服务器中的 php.ini 和 php 配置。您必须更改 php 时间限制 通过 ini_set();这样做

    【讨论】:

    • 这个答案毫无根据。这样的论点甚至没有丝毫证据。这也是一个非常糟糕的主意,因为这样每个脚本都会在没有时间限制的情况下执行,这可能会导致性能问题和服务器崩溃。
    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多