Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP max_execution_time not timing out

Tags:

php

apache

This is not one of the regular questions if sleep is counted for timeout or stuff like that. Ok, here's the problem: I've set the max_execution_time for PHP as 15 seconds and ideally this should time out when it crosses the set limit, but it doesn't. Apache has been restarted after the change to the php.ini file and an ini_get('max_execution_time') is all fine. Sometimes the script runs for upto 200 seconds which is crazy. I have no database communication whatsoever. All the script does is looking for files on the unix filesystem and in some cases re-directing to another JSP page. There is no sleep() on the script.

I calculate the total execution time of the PHP script like this:

At the start of the script I set :

$_mtime = microtime();  
$_mtime = explode(" ",$_mtime);
$_mtime = $_mtime[1] + $_mtime[0]; 
$_gStartTime = $_mtime;

and the end time($_gEndTime) is calculated similarly.
The total time is calculated in a shutdown function that I've registered:

register_shutdown_function('shutdown');
.............
function shutdown()
{
   ..............
   ..............
   $_total_time = $_gEndTime - $_gStartTime;
   ..............
   switch (connection_status ())
    {
    case CONNECTION_NORMAL:
      ....
      break;
      ....
    case CONNECTION_TIMEOUT:
      ....
      break;
      ......
    }
} 

Note: I cannot use $_SERVER['REQUEST_TIME'] because my PHP version is incompatible. That sucks - I know.

1) Well, my first question obviously is is why is my PHP script executing even after the set timeout limit?
2) Apache has the Timeout directive which is 300 seconds but the PHP binary does not read the Apache config and this should not be a problem.
3) Is there a possibility that something is sending PHP into a sleep mode?
4) Am I calculating the execution time in a wrong way? Is there a better way to do this?


I'm stumped at this point. PHP Wizards - please help.

Edit: I just found out that the stream operations are not the cause with a few logs. The delay is just random within the script even when there are no streaming operations performed. Context switching may just be the reason. But I still dont have a clear answer. I looked up Real max_execution_time for PHP on linux but Im not sure I want to try that out. Any other suggestions?

like image 556
Joey Ezekiel Avatar asked Jun 29 '12 09:06

Joey Ezekiel


People also ask

What is the best value for max_execution_time in PHP?

By default PHPs maximum execution time is set to 30 seconds of CPU time (time spent in streams and system calls are excluded from this).

How can I limit time in PHP?

Use PHP inbuilt function set_time_limit(seconds) where seconds is the argument passed which is the time limit in seconds. It is used, when the user changes the setting outside the php. ini file. The function is called within your own PHP code.

What is the maximum value of max_execution_time?

One of these rules, Max_Execution_Time, defines the maximum time a script can run for. By default, this is set to 30 seconds. If a script runs for longer than 30 seconds, it is automatically stopped, and an error is reported. You may wish to extend this time limit if you need to run large scripts on your server.


1 Answers

max_execution_time only limits script execution time itself - cpu time of your script. if the OS context switched to another process and spent some time there, it will not be counted as well. So measuring the real-world time and expecting a timeout after 30 seconds is not going to be true any given time. And of course, it disregards any system, exec or network times as well

like image 154
poncha Avatar answered Oct 31 '22 03:10

poncha