php Exec超时 [英] Php Exec timeout

查看:320
本文介绍了php Exec超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下exec()命令,末尾带有&符号,因此脚本在后台运行.但是,该脚本未在后台运行.恰好在5.6分钟后,它在浏览器中超时.另外,如果我关闭浏览器,脚本将无法继续运行.

I have the following exec() command with an & sign at the end so the script runs in the background. However the script is not running in the background. It's timing out in the browser after exactly 5.6 minutes. Also if i close the browser the script doesn't keep running.

 exec("/usr/local/bin/php -q /home/user/somefile.php &")

如果我通过命令行运行脚本,它不会超时.我的问题是我如何防止超时.如何使用exec在后台运行脚本,因此它不依赖于浏览器.我在做什么错了,应该怎么看.

If I run the script via the command line, it does not time out. My question is how do i prevent timeout. How do i run the script in the background using exec so it's not browser dependent. What am i doing wrong and what should i look at.

推荐答案

exec()函数处理已执行程序的输出,因此建议您将输出重定向到/dev/null(一个虚拟可写文件,该文件会自动丢失所有数据)您输入).

exec() function handle outputs from your executed program, so I suggest you to redirect outputs to /dev/null (a virtual writable file, that automatically loose every data you write in).

尝试运行:

exec("/usr/local/bin/php -q /home/gooffers/somefile.php > /dev/null 2>&1 &");

注意:2>&1将错误输出重定向到标准输出,而> /dev/null将标准输出重定向到该虚拟文件.

Note : 2>&1 redirects error output to standard output, and > /dev/null redirects standard output to that virtual file.

如果仍然遇到困难,可以创建一个脚本,该脚本仅执行其他脚本. exec()在执行任务时遵循一个过程,但是在任务完成时释放.如果执行的脚本仅执行另一个脚本,则任务非常快速,并且exec的释放方式相同.

If you have still difficulties, you can create a script that just execute other scripts. exec() follows a process when it is doing a task, but releases when the task is finished. if the executed script just executes another one, the task is very quick and exec is released the same way.

让我们看看一个实现.创建一个包含以下内容的exec.php:

Let's see an implementation. Create a exec.php that contains :

<?php

  if (count($argv) == 1)
  {
    die('You must give a script to exec...');
  }
  array_shift($argv);
  $cmd = '/usr/local/bin/php -q';
  foreach ($argv as $arg)
  {
     $cmd .= " " . escapeshellarg($arg);
  }
  exec("{$cmd} > /dev/null 2>&1 &");

?>

现在,运行以下命令:

exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php > /dev/null 2>&1 &");

如果有参数,也可以给它们:

If you have arguments, you can give them too :

exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php x y z > /dev/null 2>&1 &");

这篇关于php Exec超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆