php的exec执行后台进程会堵塞,卡到子进程结束才结束

php入口执行一个shell
<?php 
$a = exec("./a.sh");
var_dump($a);
 
看下a.sh 的内容
#!/bin/bash
echo "11"
(sleep 10) &
shell的sleep是后台执行的,php执行shell,php会卡住等到10s才结束,为什么?
 
---
经过朋友的提醒发现要把sleep的输出重定向到 /dev/null
修改后脚本如下
#!/bin/bash
echo "11"
(sleep 10 > /dev/null) &
 
官方也有给相关的说明,只是在很不起眼的Note 

Note:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

 

上一篇: gentoo 安装docker 折腾手记   下一篇: trafficserver 缓存时间未到,显示MISS

提交疑问

回顶部