我有一个
PHP cron作业,运行29分钟后失败.日志中的错误(/var/log/php_errors.log)是:
[01-Mar-2012 00:32:57 UTC] PHP Fatal error: Maximum execution time of 60 seconds exceeded in /path/file.php on line 2079
触发cron的crontab条目是:
00 00 * * * /usr/bin/php /path/file.php
从我的研究中我不认为这与max_execution_time配置设置有关,因为:
>我知道它的运行时间为29:18分钟(即错误信息大约超过60秒).
>从the PHP docs开始 – 从命令行运行PHP时,默认设置为0.
问:为什么脚本会提前终止?
笔记:
该脚本非常繁重,并且运行了数千个数据库查询,但我运行的是顶级且CPU负载不高.
错误日志中的行是mysql_query调用:
$sql = "SELECT SUM(amount) FROM mytab WHERE mem = '$id' AND validto > '$now'";
$res = mysql_query($sql);
> php -v
PHP 5.3.10 (cli) (built: Feb 2 2012 17:34:38)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH
> cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
更新 – 我发现为什么脚本可以运行29分钟的实时,但PHP可以退出引用执行时间低得多.
Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
(从the set_time_limit() docs开始,也在the max-execution-time docs中提到).这对我来说很重要,因为大多数脚本都是长时间运行的数据库查询和支付API调用,这些调用不会占用执行时间.