set_time_limit

(PHP 4, PHP 5, PHP 7)

set_time_limit — Limits the maximum execution time

Description

set_time_limit
( int$seconds
) : bool

Set the number of seconds a script is allowed to run. If this is reached,
the script returns a fatal error. The default limit is 30 seconds or, if
it exists, the max_execution_time value defined in the
php.ini.

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.

Parameters

seconds

The maximum execution time, in seconds. If set to zero, no time limit
is imposed.

Return Values

Returns TRUE on success, or FALSE on failure.

Notes

Warning

This function has no effect when PHP is running in safe mode. There is
no workaround other than turning off safe mode or changing the time limit
in the php.ini.

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.

I was having trouble with script timeouts in applications where the user prompted long running background actions. I wrote this cURL/CLI background script that solved the problem when making requests from HTTP.

Both set_time_limit(...) and ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:

Documentation states: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.

If I have a long running script and i want a exact time limit, I set this as near as possible to the first line.

If set_time_limit has been previously called in the script, the result will be the value which was passed to set_time_limit (and not, as the function name "ini_get" appears to suggest, the value from the php.ini file).

if you are running a script that needs to execute for unknown time, or forever.. you may useset_time_limit(0);...........and at the end of the script use flush() function to tell phpto send out what it has generated.

Timeouts after five minutes in IIS on Windows are caused by an inherited CGI Timeout value of 300 seconds. This is not a PHP problem. The fix is to add custom values for the files or directories that need longer to run.

In IIS 5.0 or 7.0 (beta as of this note), you can change this value on a fairly granular level using IIS Manager, under (roughly) YOURSITE -> Properties -> Home Directory -> Configuration (button) -> Options, but in IIS 6.0, this functionality is turned off (!), so you have to get into the Metabase.

Find the site number in Metabase Explorer (e.g., 12345678), then from CMD prompt:

[get to the scripts dir]cd C:\Inetpub\AdminScripts

[this for each subdirectory from off the site root]cscript adsutil.vbs CREATE W3SVC/12345678/root/"MY SUBDIRECTORY" IIsWebDirectory

Regarding what 'nytshadow' said, it's important to realize that max-execution-time and the set_time_limit functions measure the time that the CPU is working on the script. If the script blocks, IE: for input, select, sleep, etc., then the time between blocking and returning is NOT measured. This is the same when running scripts from the command line interface. So if you've got a log parser written in PHP that tails a file, that program WILL fail eventually. It just depends how long it takes to read in enough input to process for 30 seconds.

If you're writing a command line script that should run infinitely, setting max-execution-time to 0 (never stop) is HIGHLY recommended.

If you're using PHP_CLI SAPI and getting error "Maximum execution time of N seconds exceeded" where N is an integer value, try to call set_time_limit(0) every M seconds or every iteration. For example: