PHP script runs much longer than max_execution_time?

2.2k views Asked by At

PHP is running as an Apache module.

The script start with a: ini_set('max_execution_time', 300);

What it does is basically connecting to a database, doing a big SELECTquery and looping through the results, writing them to a file and echoing a "result ok" after each write with explicit flush();

There is no sleep() call.

This is a "test" script made from a co-worker of mine for backup purposes and is intended to run up to a few hours! I thought I was aware of script execution time limit and thought his script would time out after 300 seconds... But it didn't !

It's invoked from a web browser. The page is left open and we can see the results being echoed in real-time.

Why doesn't it time out? Even stranger, one of the test issued a "Maximum execution time of 300 seconds exceeded" but this appeared at least after 2 hours of execution!

What's going on here? Is there something to understand between max_execution_time and flush() or a browser window being kept opened?

2

There are 2 answers

0
PiranhaGeorge On BEST ANSWER

The only thing I can think of that may cause this is if PHP is running in safe mode. ini_get('max_execution_time') should tell you if it's actually being set.

Edit: Spotted your comment above.

echo ini_get('max_execution_time'); // says 300

If 300 is reported, and your not on Windows, @mishu is likely right. Your SELECT query is probably taking hours.

1
mishu On

As you can see on the man page for the set_time_limit function, here the total execution time you are setting only affects the actual script. The time spent on database queries or any other external calls is not counted (if the OS is not Windows).