PHP内置的webserver似乎没有正确地处理shell_exec()的后台进程;请求挂起直到后台进程完成,即使它显式地放置在带有&的后台。
示例:
$ ls
runit.php
$ cat runit.php
<?php
echo "Here we are\n";
shell_exec("sleep 5 &");
echo "and the command is done\n";
?>
$ 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.然后在另一个壳里:
$ GET http://localhost:7891/runit.php
(...waits five seconds...)
Here we are
and the command is done这种情况不应该发生,而且如果使用生产级的and服务器,也不会发生这种情况。有办法解决这个问题吗?
(注:这不是冲水的问题。在第一个回显之后添加flush()并不能使其发生,并且请求仍然挂起,直到后台进程完成。)
发布于 2015-05-18 19:40:50
我是被PHP确认为错误,但不会在内置的webserver中修复。。但是,bug报告也提出了解决办法;在响应上设置一个正确的Content-Length,然后接收浏览器将在收到大量数据后在客户端关闭请求,从而解决问题。
发布于 2015-05-18 18:27:20
你的选择是:
1)使用单独的线程运行进程
<?php
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
sleep(1);
print "In child $i\n";
exit($i);
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
?>2)您可以在shell exec的末尾追加'> /dev/null 2>/dev/null &',这也将消除所有输出,但它将运行命令。让它看起来像
shell_exec('sleep 5 > /dev/null 2>/dev/null &');
https://stackoverflow.com/questions/30310327
复制相似问题