IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    linuxphp@qq.com(keminar)发表于 2016-08-24 18:41:00
    love 0
    贵贵的博客 ( http://blog.linuxphp.org/ ) :
    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.

     



沪ICP备19023445号-2号
友情链接