title: PHP cron job premature termination due to Maximum execution time fatal error
body: <p>I have a PHP cron job that is failing after running for 29 minutes. The error in the log (<code>/var/log/php_errors.log</code>) is:</p>
  
  <pre class="lang-none prettyprint-override"><code>[01-Mar-2012 00:32:57 UTC] PHP Fatal error: Maximum execution time of 60 seconds exceeded in /path/file.php on line 2079
  </code></pre>
  
  <p>The crontab entry that triggers the cron is:</p>
  
  <pre class="lang-none prettyprint-override"><code>00 00 * * * /usr/bin/php /path/file.php
  </code></pre>
  
  <p>From my research I don't think this is related to the <code>max_execution_time</code> config setting because:</p>
  
  <ol>
  <li>I know for a fact it ran for 29:18 mins (i.e much more than 60s like the error message).</li>
  <li>From <a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time" rel="nofollow">the PHP docs</a> - When running PHP from the command line the default setting is 0.</li>
  </ol>
  
  <p><strong>Q: Why is the script terminating early?</strong></p>
  
  <hr>
  
  <p>Notes:</p>
  
  <p>The script is very heavy, and does run many thousands of DB queries, but I was running <code>top</code> and the CPU load wasn't high.</p>
  
  <p>The line from the error log is a <code>mysql_query</code> call:</p>
  
  <pre><code>$sql = "SELECT SUM(amount) FROM mytab WHERE mem = '$id' AND validto &gt; '$now'";
  $res = mysql_query($sql);
  </code></pre>
  
  <hr>
  
  <pre class="lang-none prettyprint-override"><code>&gt; 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
  
  &gt; cat /etc/redhat-release
  Red Hat Enterprise Linux Server release 5.7 (Tikanga)
  </code></pre>
  
  <hr>
  
  <p><em>Update</em> - I found out why the script can run for 29 minutes of real time but PHP can exit quoting execution time much lower.</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>(from <a href="http://www.php.net/manual/en/function.set-time-limit.php#refsect1-function.set-time-limit-notes" rel="nofollow">the set_time_limit() docs</a>, but also mentioned in <a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time" rel="nofollow">the max-execution-time docs</a>). 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.</p>