【问题标题】:Shell_exec php with nohupShell_exec php 与 nohup
【发布时间】:2011-06-23 05:22:06
【问题描述】:

我想有很多类似的帖子,但我搜索了一下还没有找到解决方案。

基本上,我试图在后台运行两个脚本。当我在命令行中运行它们时,我在调用我的第一个脚本后看到:

/usr/bin/nohup php script.php > nohupoutput.log & echo $!

我尝试过...script.php > /dev/null &,结果相同。我明白了:

/usr/bin/nohup: ignoring input and redirecting stderr to stdout

我忽略并运行第二个。我注意到它似乎挂在那里,按 Enter 将我带回 machine:~folder>

/usr/bin/nohup php script2.php > nohupoutput.log & echo $!

两个脚本都有效。然后我尝试将其转换为 shell_exec 命令,但似乎没有任何效果。我怀疑ignoring input 位会造成困难,但我不确定。无论如何,以下不起作用。它只是挂在浏览器中:

$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');

【问题讨论】:

    标签: php exec shellexecute shell-exec nohup


    【解决方案1】:

    试试:

    $output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
    

    或者:

    exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
    

    【讨论】:

    • 当我在命令行上同时尝试这两种方法时,我得到了一个不明确的输出重定向。
    • 您使用的是哪个操作系统,以及 php 版本?这是一个奇怪的。
    【解决方案2】:

    这应该有效:

    shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');
    

    【讨论】:

      【解决方案3】:
      <?php
      function execInBackground($cmd) {
          if (substr(php_uname(), 0, 7) == "Windows"){
              pclose(popen("start /B ". $cmd, "r")); 
          }
          else {
              exec($cmd . " > /dev/null &");  
          }
      }
      
      // take note: to get your PHP_PATH, try looking at your phpinfo :)
      echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
      ?>
      

      【讨论】:

      • popen('nohup ... &amp;', 'r') 是在 OS X 和 Heroku 中对我有用的唯一方法。用于运行分离的后台进程的大量其他解决方案,包括。 Symfony、Cocur 等失败(阻止执行或在父节点死亡时死亡)。
      【解决方案4】:

      首先将您的 php 命令放入 shell 文件脚本中,例如myscript.sh

      #!/bin/bash
      # myscript.sh file
      php script.php
      

      使用 myscript.sh 运行 nohup:

      sudo nohup ./myscript.sh &
      

      用ps验证:

      ps aux | grep myscript.sh
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-09
        • 2011-10-28
        • 1970-01-01
        • 2017-05-24
        • 1970-01-01
        • 2018-08-21
        • 2011-08-13
        相关资源
        最近更新 更多