【问题标题】:Run php script in background using exec使用 exec 在后台运行 php 脚本
【发布时间】:2013-01-08 11:03:37
【问题描述】:

我有一个 php 文件来创建一个目录。我希望该 php 文件在后台执行。我写了 2 个如下文件。

file1.php

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);
echo 'calling file1';
if(!is_dir('testdir')){
   mkdir('testdir');
}
?>

index.php

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);
$i = '';
exec("php file1.php $i > test.txt &");
?>

我希望在通过浏览器执行 index.php 时创建一个目录。我认为我在编写 exec() 函数时犯了错误。谁能告诉我会怎么样?

【问题讨论】:

  • 您可以使用 curl_execute 在您的服务器上打开 php 文件。它比 exec 安全得多。 (关于注入 $i...
  • @Amina:多么奇怪的建议:-S
  • @Amina:$i 如何被“注入”的具体示例?
  • 您是否尝试过通过 ssh shell 执行命令?设备是否全局知道“php”而没有任何路径?脚本(file1.php)的路径是否正确?最好的事情是:查看系统的 error.log 并在此处发布这些行
  • 我正在使用 exec 因为我想异步执行 file1.php。我使用的是 PHP 5.2.4 版。我们可以使用 curl_execute 异步调用文件吗?PHP 5.4.2 版本是否支持它

标签: php exec


【解决方案1】:

所以,答案是使用 CURL,因为您不需要为此使用任何 PHP 二进制文件。

如果您不希望 file1.php 等到 file2.php 完成,请为 file1 上的 cUrl 请求添加超时:

$curlcontext,CURLOPT_CONNECTTIMEOUT_MS,100;

现在,将它添加到 file2.php 很重要:

ignore_user_abort (true);

这意味着file2.php在超时或file1.php之后不会终止

http://php.net/manual/en/function.ignore-user-abort.php http://php.net/manual/en/function.curl-setopt.php

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多