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条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献