duanmao2774 2015-05-18 18:22
浏览 217
已采纳

使用PHP内置Web服务器中的shell_exec运行后台进程

The PHP built-in webserver does not seem to handle shell_exec()ing background processes properly; the request hangs until the background process is complete, even if it's explicitly placed in the background with &.

Example:

$ ls
runit.php
$ cat runit.php 
<?php
echo "Here we are
";
shell_exec("sleep 5 &");
echo "and the command is done
";
?>
$ php -S localhost:7891
PHP 5.5.9-1ubuntu4.9 Development Server started at Mon May 18 19:20:12 2015
Listening on http://localhost:7891
Press Ctrl-C to quit.

and then in another shell:

$ GET http://localhost:7891/runit.php
(...waits five seconds...)
Here we are
and the command is done

This shouldn't happen, and indeed doesn't if one uses a production-grade webserver. Is there any way to work around it?

(Note: this is not a flushing problem. Adding flush() after the first echo doesn't make it happen, and the request still hangs until the background process is complete.)

  • 写回答

2条回答 默认 最新

  • duandeng7132 2015-05-18 19:40
    关注

    This is acknowledged as a bug by PHP, but won't be fixed in the built-in webserver. However, the bug report does also suggest a workaround; set a correct Content-Length on the response, and then the receiving browser will close the request at the client end after receiving that much data, thus working around the problem.

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料