Nick Shears
Nick Shears

Reputation: 1133

Potential reasons for PHP script not ending after time out specified?

I'm having a bit of a server issue.

using the following random script to just produce a time out.

set_time_limit(1);
$x = 0;
while ($x < 1000)
{

}

The problem I'm facing is the server is actually taking around 10-25 seconds or so to finish the script and produce the fatal error "Fatal error: Maximum execution time of 1 second exceeded"

whereas on my local machine, the error appears almost instantly, I've disabled custom error handlers on production server as I thought it might be that but I'm still facing the same issue.

Any ideas as to what could be causing this?

Edit

Just to clarify, max execution time IS being set successfully on production and the error message is just the same as local - "Fatal error: Maximum execution time of 1 second exceeded"

It just takes around 10-25 seconds for the error to eventually appear on production.

Upvotes: 0

Views: 393

Answers (3)

BT643
BT643

Reputation: 3845

Okay, I assume your production server is Linux/Unix and your local server is Windows? They use set_time_limit differently apparently:

From this URL: http://www.soliantconsulting.com/blog/2010/02/phps-max_execution_time-different-on-windows-and-linux

On Windows, the PHP INI variable, "max_execution_time," is the maximum clock time between when the PHP process starts and when it finishes.

On Linux and OS X, the same variable represents the maximum CPU time that the PHP process can take. CPU time is less than clock time because processes on Linux (and OS X) contend for CPU time, and sometimes sit idle.

For example, if you have a PHP program which makes a request to a web service, like FileMaker's Custom Web Publishing, on Linux, the time the PHP program waits for a response is not counted, while on Windows, it does. You can see this by running the script:

<?php
    set_time_limit(15); /* Sets max_execution_time */
    sleep(30); /* Wait thirty seconds */
    echo "Hello World!\n";
?>

If you run this on Windows, it will fail with an error message, but on Linux or OS X, the time spent sleeping is time the process is "idle," so the execution time is considered to be almost zero.

Upvotes: 1

BT643
BT643

Reputation: 3845

set_time_limit(1);
echo "Max Execution Time: ".ini_get('max_execution_time');

That should tell you if your set call is even doing anything. If you're on shared hosting you may not even be allowed to change it.

Upvotes: 0

Raggamuffin
Raggamuffin

Reputation: 1760

Your production server may have disabled setting INI settings from PHP scripts, which is good practice. Instead of using set_time_limit(1) in your script, you should set this in php_ini.

Upvotes: 1

Related Questions