On Ubuntu 10.10 with Apache2 + php, the script
<?
system("sleep 10000");
?>
does not time out. Both max_execution_time and TimeOut do not apply.
Is there any way to ensure that a script will be halted?
TimeOut
has no effect on mod_php.max_execution_time
only affects the execution time of the script itself. System calls using system()
or exec()
do not count towards the execution time except on Windows.Alvosu's answer is nearly right - but a bit terse.
PHP's sleep() is a simple wrapper under the OS's scheduler API. When its called, php stops running - so the PHP can't check if the clock has run out until the sleep time expires and the scheduler adds it back onto the run queue, and the CPU gets round to processing the next instruction.
While this example is rather artificial (why would you ever write a script like that knowing you'd got a timeout running) there are many scenarios where the thread of execution passes outside of the PHP interpreter, and it therefore does not get a chance to check its stopwatch - e.g. when the script starts other processes, or the thread is in an extension - such as waiting for a db query to return. The problem can be avoided by asking the system to send a signal back to the PHP code at a defined interval (pcntl_alarm) after you define a signal handler (pcntl_signal).
With Apache and mod_php you'll have an issue here. Other methods such as loading php via fcgid or fastcgi would give you greater control over process management / execution time / timeouts / idle time / etc.