dslf46995 2012-09-28 07:18
浏览 8
已采纳

在调用exec()之后,如何强制PHP脚本继续使用脚本?

I'm using the exec function in PHP to run a command. The command I'm running can often take quite a bit of time and I don't have any need to read it's output. Is there a simple way of telling PHP not to wait for the exec command to finish before moving on with the rest of the script?

  • 写回答

3条回答 默认 最新

  • duanchigeng4313 2012-09-28 07:44
    关注
    // nohup_test.php:
    
    // a very long running process
    $command = 'tail -f /dev/null';
    exec("nohup $command >/dev/null 2>/dev/null &"); // here we go
    printf('run command: %s'.PHP_EOL, $command);
    echo 'Continuing to execute the rest of this script instructions'.PHP_EOL;
    
    for ($x=1000000;$x-->0;) {
      for ($y=1000000;$y-->0;) {
        //this is so long, so I can do ps auwx | grep php while it's running and see whether $command run in separate process
      }
    }
    

    run nohup_test.php:

    $ php nohup_test.php
    run command: tail -f /dev/null
    Continuing to execute the rest of this script instructions
    

    Let's find out pids of our processes:

    $ ps auwx | grep tail
    nemoden   3397  0.0  0.0   3252   636 pts/8    S+   18:41   0:00 tail -f /dev/null
    $ ps auwx | grep php
    nemoden   3394 82.0  0.2  31208  6804 pts/8    R+   18:41   0:04 php nohup_test.php
    

    as you can see, pid is different and my script is running without waiting for tail -f /dev/null.

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

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题