1

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?

3 Answers 3

8
  • 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.
1

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).

6
  • @symcbean where MathGladiator call PHP's sleep()?
    – alvosu
    Feb 3, 2011 at 15:14
  • This is very interesting. I'll have to look into this as a more general solution. If I can use this to make it so I can basically kill anything I want, then I'm in business. Ideally, I would have liked a solution that was in Apache2 to guarantee this. Feb 5, 2011 at 20:19
  • @symcbean system("sleep 10000") not equally sleep(). Read the code in question.
    – osdyng
    Feb 6, 2011 at 16:03
  • @osdyng - same thing just via a different route - the sleep commeand (more likely the sheell alias) is just a wrapper around the OS call.
    – symcbean
    Feb 6, 2011 at 22:51
  • @symcbean try run system("while true; cat big_file > /dev/null;done"), max_execution_time don't work, but no sleep() call
    – alvosu
    Feb 7, 2011 at 7:28
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .