【问题标题】:php shell exec wget to run not in backgroundphp shell exec wget不在后台运行
【发布时间】:2012-07-05 01:35:40
【问题描述】:

我想如下运行wget

shell_exec('wget "'http://somedomain.com/somefile.mp4'"');
sleep(20);
continue my code...

我想要的是让 PHP 等待 shell_exec wget 文件下载完成,然后再继续执行其余代码。我不想等待设定的秒数。

我该怎么做,因为当我运行 shell_exec wget 时,文件将开始下载并在后台运行,PHP 将继续。

【问题讨论】:

    标签: php shell exec wget


    【解决方案1】:

    您的网址是否包含 & 字符?如果是这样,您的 wget 可能会进入后台,而 shell_exec() 可能会立即返回。

    例如,如果 $url 是“http://www.example.com/?foo=1&bar=2”,您需要确保在命令行中传递它时是单引号:

    shell_exec("wget '$url'");
    

    否则 shell 会误解 &。

    转义命令行参数通常是一个好主意。最全面的方法是使用 escapeshellarg():

    shell_exec("wget ".escapeshellarg($url));
    

    【讨论】:

      【解决方案2】:

      shell_exec 确实等待命令完成 - 所以你根本不需要 sleep 命令:

      <?php
      shell_exec("sleep 10");
      ?>
      
      # time php c.php
         10.14s real     0.05s user     0.07s system
      

      我认为你的问题很可能是这一行的引号:

      shell_exec('wget "'http://somedomain.com/somefile.mp4'"');
      

      应该是

      shell_exec("wget 'http://somedomain.com/somefile.mp4'");
      

      【讨论】:

      • 它不等待我的情况,它只是运行下一行代码。
      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2019-01-04
      相关资源
      最近更新 更多