运行PHP脚本作为cron作业 - 超时问题?

时间:2021-08-04 08:01:34

I am coding a php script that does some back end stuff and needs to run every 8 hours or so. The script takes a while to execute. For the hell of it, I tried it from my browser and the connection to the server gets reset well before the script terminates. My question is - if I run it directly, ie. php -a file.php as a cron job, are there any internal time constraints on execution? This script may take 2-5 minutes to complete and cannot be interrupted. I've never done this before so I am not sure if php has quirks when running heavy scripts.

我正在编写一个PHP脚本来做一些后端的东西,需要每8小时左右运行一次。该脚本需要一段时间才能执行。对于它的地狱,我在我的浏览器中尝试了它,并且在脚本终止之前很快就重置了与服务器的连接。我的问题是 - 如果我直接运行,即。 php -a file.php作为cron作业,执行时是否有内部时间限制?此脚本可能需要2-5分钟才能完成,并且无法中断。我之前从未这样做过,所以我不确定php在运行繁重的脚本时是否有怪癖。

3 个解决方案

#1


24  

As said before, CLI scripts by default have no time limit.

如前所述,默认情况下CLI脚本没有时间限制。

But I would also like to mention an alternative to your cron job approach:
You can fork a CLI PHP script from a PHP script under webserver control. I have done this many times. It is especially useful if you have a script with long execution time which must be triggered by some website user action (e.g. building a very large archive file and send a download link by email when the file is complete). I usually fork a CLI script from a webserver PHP script using the popen() function. This allows to nicely transfer parameters to the new script instance like this:

但我还想提一下您的cron作业方法的替代方法:您可以在Web服务器控件下从PHP脚本派生CLI PHP脚本。我做了很多次。如果您的脚本具有较长的执行时间,必须由某些网站用户操作触发(例如,构建一个非常大的存档文件并在文件完成时通过电子邮件发送下载链接),这将非常有用。我通常使用popen()函数从Web服务器PHP脚本分叉CLI脚本。这样可以很好地将参数传递给新的脚本实例,如下所示:

$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
  die('Could not open bgrnd process');
}else{
  // send params through stdin pipe to bgrnd process:
  $p1 = serialize($param1);
  $p2 = serialize($param2);
  $p3 = serialize($param3);
  fwrite($bgproc, $p1 . "\n" . $p2 . "\n" . $p3 . "\n");
  pclose($bgproc);
}

In the CLI script you would receive these params like this...

在CLI脚本中,您将收到这样的参数...

$fp = fopen('php://stdin', 'r');
$param1 = unserialize(fgets($fp));
$param2 = unserialize(fgets($fp));
$param3 = unserialize(fgets($fp));
fclose($fp);

...and do anything with them that would take to long under webserver control.

...并且在Web服务器控制下需要做很长时间。

This technique works equally well in *nix and Windows environments.

这种技术在* nix和Windows环境中同样有效。

#2


4  

No there are no time limits in php itself when executing php from the command line.

从命令行执行php时,php本身没有时间限制。

But there can be other timeouts, like connections to mysql. So if you have a mysql connection in your code, make sure to keep it alive or set your mysql timeout to something high enough to run your code. Another thing: I've seen some webhosting providers killing php apps running more then a few minutes. So make sure your provider does not do that.

但是可能还有其他超时,例如与mysql的连接。因此,如果您的代码中有mysql连接,请确保将其保持活动状态或将mysql超时设置为足以运行代码的高度。另一件事:我看到一些网站主机提供商杀死运行超过几分钟的PHP应用程序。因此,请确保您的提供商不这样做。

#3


3  

As default, PHP scripts timesout after 30 seconds which can be overridden by editing the PHP.ini or by adding this at the top of your script.

默认情况下,PHP脚本在30秒后超时,可以通过编辑PHP.ini或在脚本顶部添加它来覆盖它。

set_time_limit(0);

This sets unlimited execution time to your script, that is, it never ends unless the scripts finish execution, or the server goes down, or the file is deleted or any fatal error comes.

这为脚本设置了无限的执行时间,也就是说,除非脚本完成执行,或者服务器出现故障,或者文件被删除或出现任何致命错误,否则它永远不会结束。

Additionally,

You can add this to your script and open it in your browser to initiate the script, and it will run as if you are opening it in a browser and keeping the browser open.

您可以将其添加到脚本中并在浏览器中打开以启动脚本,它将像在浏览器中打开并保持浏览器打开一样运行。

ignore_user_abort();

It simply runs the script in background. These both would be useful for you.

它只是在后台运行脚本。这些都对你有用。

ADD : When the scripts are run from the commandline,Cli, the default timeout is 0.(No Timeout)

ADD:从命令行Cli运行脚本时,默认超时为0.(无超时)

#1


24  

As said before, CLI scripts by default have no time limit.

如前所述,默认情况下CLI脚本没有时间限制。

But I would also like to mention an alternative to your cron job approach:
You can fork a CLI PHP script from a PHP script under webserver control. I have done this many times. It is especially useful if you have a script with long execution time which must be triggered by some website user action (e.g. building a very large archive file and send a download link by email when the file is complete). I usually fork a CLI script from a webserver PHP script using the popen() function. This allows to nicely transfer parameters to the new script instance like this:

但我还想提一下您的cron作业方法的替代方法:您可以在Web服务器控件下从PHP脚本派生CLI PHP脚本。我做了很多次。如果您的脚本具有较长的执行时间,必须由某些网站用户操作触发(例如,构建一个非常大的存档文件并在文件完成时通过电子邮件发送下载链接),这将非常有用。我通常使用popen()函数从Web服务器PHP脚本分叉CLI脚本。这样可以很好地将参数传递给新的脚本实例,如下所示:

$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
  die('Could not open bgrnd process');
}else{
  // send params through stdin pipe to bgrnd process:
  $p1 = serialize($param1);
  $p2 = serialize($param2);
  $p3 = serialize($param3);
  fwrite($bgproc, $p1 . "\n" . $p2 . "\n" . $p3 . "\n");
  pclose($bgproc);
}

In the CLI script you would receive these params like this...

在CLI脚本中,您将收到这样的参数...

$fp = fopen('php://stdin', 'r');
$param1 = unserialize(fgets($fp));
$param2 = unserialize(fgets($fp));
$param3 = unserialize(fgets($fp));
fclose($fp);

...and do anything with them that would take to long under webserver control.

...并且在Web服务器控制下需要做很长时间。

This technique works equally well in *nix and Windows environments.

这种技术在* nix和Windows环境中同样有效。

#2


4  

No there are no time limits in php itself when executing php from the command line.

从命令行执行php时,php本身没有时间限制。

But there can be other timeouts, like connections to mysql. So if you have a mysql connection in your code, make sure to keep it alive or set your mysql timeout to something high enough to run your code. Another thing: I've seen some webhosting providers killing php apps running more then a few minutes. So make sure your provider does not do that.

但是可能还有其他超时,例如与mysql的连接。因此,如果您的代码中有mysql连接,请确保将其保持活动状态或将mysql超时设置为足以运行代码的高度。另一件事:我看到一些网站主机提供商杀死运行超过几分钟的PHP应用程序。因此,请确保您的提供商不这样做。

#3


3  

As default, PHP scripts timesout after 30 seconds which can be overridden by editing the PHP.ini or by adding this at the top of your script.

默认情况下,PHP脚本在30秒后超时,可以通过编辑PHP.ini或在脚本顶部添加它来覆盖它。

set_time_limit(0);

This sets unlimited execution time to your script, that is, it never ends unless the scripts finish execution, or the server goes down, or the file is deleted or any fatal error comes.

这为脚本设置了无限的执行时间,也就是说,除非脚本完成执行,或者服务器出现故障,或者文件被删除或出现任何致命错误,否则它永远不会结束。

Additionally,

You can add this to your script and open it in your browser to initiate the script, and it will run as if you are opening it in a browser and keeping the browser open.

您可以将其添加到脚本中并在浏览器中打开以启动脚本,它将像在浏览器中打开并保持浏览器打开一样运行。

ignore_user_abort();

It simply runs the script in background. These both would be useful for you.

它只是在后台运行脚本。这些都对你有用。

ADD : When the scripts are run from the commandline,Cli, the default timeout is 0.(No Timeout)

ADD:从命令行Cli运行脚本时,默认超时为0.(无超时)