Pergunta

Script runs on shared hosting and obeys the root php.ini file. It uses PHP 5.4 so there's no safe_mode and it's not using .htaccess files. I asked support if they have any limits, she said the timeout is two hours on their side. On localhost it doesn't stop but ignores my limits.

This example stops after 601-606sec (~10min) instead of 1000sec:

// For immidiately printing in browser
header('Content-Type:text/html; charset=UTF-8');

$limit = 1000;

set_time_limit($limit);
ini_set('max_execution_time', $limit);
ini_set('max_input_time', $limit);
ini_set('memory_limit', $limit . 'M');
ini_set('max_input_time', $limit);
ini_set('post_max_size', $limit);
ini_set('file_uploads', $limit);
ini_set('upload_max_filesize', $limit);

$start = microtime(1);

// Every second prints elapsed seconds
for ($i=0; $i<1000; $i++){
    echo str_pad(round(microtime(1)-$start), 4096);
    flush();
    sleep(1);
}
  1. Most important question is why it stops? I don't need 2 hours but 20-30min will be nice.
  2. Why it ignores my limits? I can change $limit to '1' but it will change nothing.
Foi útil?

Solução

First about timeouts:

set_time_limit() and max_execution_time don't count sleep(), system(), file_get_contents() and DB calls. That's why I thought the script ignores my limits.

I talked again to support and turned out that what stops the script is not Apache timeout but «something» that monitors scripts and stops it after 10 minutes for shared IP customers. I asked him for full list of limits:

Type       | Dedicated | Shared
-----------+-----------+-----------
Apache/Web | 12 hours  | 10 minutes
SSH/Shell  | 2 hours   | 1 hour
Cron Jobs  | 1 hour    | 30 minutes
Daemons    | Unlimited | 10 minutes

That's why it ran fine from the cron.

Outras dicas

Such timeouts generally occur when there is a CGI timeout value, which is not a problem originating from php.

Try this:

set_time_limit(0);  // run foorreeveeerr
for ($i=0; $i<1000; $i++){
    echo str_pad(round(microtime(1)-$start), 4096);
    flush();
    sleep(1);
    set_time_limit(0); // if PHP_CLI SAPI and having error messages
}

Note that this is not a recommended way, which without control can cause script run too long... But to diagnose the source of problem, this may come handy.

Setting time limit to 0, is a way of telling that there is no time limit.

If your script is running on a IIS server, look CGI timeout settings which affects the script time out, for setting it please look here

If on Apache, check Apache's timeout value in the httpd.conf.

Although support says the sky is the limit, things can happen ;).

Web server might force you out with a HTTP timeout. Ask support about this. Because you are monitoring from a browser I guess.

You can use (better do it) ini_get() function to verify the values of:

 ini_get('safe_mode');

and

 ini_get('max_execution_time'); 

etc. Specially the latter before and after setting it with

 ini_set('max_execution_time');

To verify the setting at least seems to be successfull.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top