douyasihefu6214 2016-04-05 14:26
浏览 66
已采纳

PHP CLI控制台关闭时出错

I'm running a loop that executes the same application once the round is finished, using proc_open, like this:

$description = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "proc_open_errors", "a+")
);

$command = "start /min php $operador_php";
$process = proc_open($command, $description, $pipe);

if (is_resource($process)) {
    print_r($process);
    sleep(5);
    fclose($pipe[0]);
    fclose($pipe[1]);
    $process_response = proc_close($process);
    echo $process_response;
}

The issue I have is that after some hours running, one of the executions may fail in the middle, breaking the whole loop. And it closes itself (on Windows) so I can't see what the problem is.

Is there any way to prevent the PHP CLI from closing itself in case there's an issue? or should I rather use a sequence of "if /else" to check if there's any problem during the execution? Any other idea?

  • 写回答

2条回答 默认 最新

  • dongnue4923 2016-04-05 14:37
    关注

    You have options, but it depends on how you're executing it.

    One option is to configure error handling in the php script. At least you can catch generic exceptions and log them before failing. Better yet, in many cases you might be able to recover from errors. Either way, http://php.net/manual/en/language.exceptions.php might be an interesting read.

    A simpler approach, and perhaps a good place to start, would be to redirect output from the script into another file. Redirect Windows cmd stdout and stderr to a single file shows an easy invocation - works the same in unix. This way the window will still go away but you'll be able to view the logs separately. This is definitely a best practice when executing unattended scripts in any OS. Just make sure to handle the output file size reasonably ( if the php script is going to run forever, you'll have to do something more clever to keep the log from filling up your hard drive).

    Finally, if you manually invoke the php script from the command line, I don't think the command line will exit when the script exits, but I guess I wouldn't be shocked if it did (as opposed to having windows auto-magically launch a shell for the process and then auto-magically close it when done ).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀