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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持