I have a PHP cron job that is failing after running for 29 minutes. The error in the log (/var/log/php_errors.log
) is:
[01-Mar-2012 00:32:57 UTC] PHP Fatal error: Maximum execution time of 60 seconds exceeded in /path/file.php on line 2079
The crontab entry that triggers the cron is:
00 00 * * * /usr/bin/php /path/file.php
From my research I don't think this is related to the max_execution_time
config setting because:
Q: Why is the script terminating early?
Notes:
The script is very heavy, and does run many thousands of DB queries, but I was running top
and the CPU load wasn't high.
The line from the error log is a mysql_query
call:
$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)
Update - I found out why the script can run for 29 minutes of real time but PHP can exit quoting execution time much lower.
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.
(from the set_time_limit() docs, but also mentioned in the max-execution-time docs). This was relevant for me because most of the script was long running db queries and payment API calls that won't have been clocking up execution time.
Well, you can set a bigger value for time limit, or you can set it unlimited using set-time-limit():
<?php set_time_limit(0); ?>
but acctually I use this too at the start of the script
ignore_user_abort(1);
If you get PHP Fatal error: Maximum execution time of 60 seconds exceeded
then for sure some piece of running PHP code runs statement set_time_limit(60)
somewhere. PHP CLI mode may default to no timelimit but if any code path ever sets time limit, it will be honored. The reason that PHP did run for close to half an hour is because set_time_limit
sets limit for CPU time and if the process is I/O limited or waiting for other processes, the total CPU usage would hit 60 second mark much later in real time clock.
Try searching all your source code for set_time_limit
. If you don't find anything then add set_time_limit(0)
at the start of the script to make sure that the 60 second limit does not come from locally modified configuration file. For example, on Ubuntu LTS the PHP CLI configuration is defined in /etc/php5/cli/php.ini
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With