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 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码