Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: set_time_limit() seems to have no effect whatsoever

Tags:

php

timeout

I'm trying to use PHP's set_time_limit() in a normal PHP apache script (not CLI) to limit its max running time. It's not doing anything with system calls or anything similar, we're not using safe mode (we're the owners but not the admins of the server). This is PHP 5.2.4 on Linux (Ubuntu).

Simple test case:

ini_set('max_execution_time', 1);
set_time_limit(1);
$i=0;
while ($i++<100000000000) {} // or anything other arbitrary that takes time
die('Done');

Expected result: something relating to the script execution time being exceeded

Actual result: Done is printed.

Am I doing something obvious wrong? (By the way, yes, the script really needs to and can run for too long and need to be aborted, it's unfortunately not something that can be avoided. That is not the point here.)

like image 442
StrikerNL Avatar asked Mar 30 '11 08:03

StrikerNL


People also ask

How to use set_ time_ limit in PHP?

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

What is the Maximum execution time in PHP?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.


1 Answers

Quoting from the manual:

Note:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. 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. This is not true on Windows where the measured time is real.

sleep() is among those functions that do not affect the running time of the script

see MPHH's comment on the sleep() page of the manual

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.

like image 195
Mark Baker Avatar answered Oct 21 '22 07:10

Mark Baker