3

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.
mu3
  • 739
  • 1
  • 7
  • 19
  • 1
    Are you running it through a browser? If so, the web server probably also has a timeout – Mark Baker Apr 21 '13 at 11:39
  • Yes, through a browser. I run it now with cron and it worked! Is there any timeout in a browser? – mu3 Apr 21 '13 at 13:31
  • No idea about a browser; but webservers typically have a timeout – Mark Baker Apr 21 '13 at 13:34
  • Is there any way to adjust the web server timeout on shared hosting? – mu3 Apr 21 '13 at 14:03
  • That all depends on your host; but if it takes that long to run you shouldn't be leaving the browser user waiting that long.... they'll just get bored, and abort – Mark Baker Apr 21 '13 at 14:05
  • I run in it a browser for dubugging (this way I can see the progress). – mu3 Apr 21 '13 at 20:11
  • But there is another problem – why does it ignore my limits? Even when runs without Apache? – mu3 Apr 21 '13 at 20:27
  • If you're running it in a browser, then you're running it through a webserver. You mention that it runs OK through cron, so it does work when you run it without Apache... so what makes you say it's ignoring your limits then. – Mark Baker Apr 21 '13 at 21:50
  • And it's perfectly possible to run it from the command line for debugging (you can still echo output when you're running from the command line – Mark Baker Apr 21 '13 at 21:53

2 Answers2

4

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.

mu3
  • 739
  • 1
  • 7
  • 19
2

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.

Volkan
  • 2,212
  • 1
  • 14
  • 14
  • Doesn't help. It's running on Apache but since this is shared hosting I have not access to httpd.conf. Can I change it in .htaccess? And is it possible to check what are the limits in httpd.conf with php? To make sure support knows what they are talking about… – mu3 Apr 21 '13 at 12:25
  • Edited the answer... Now thats all my knowledge about the topic,I hope you will find a solution soon and I wish you luck. – Volkan Apr 21 '13 at 12:44
  • safe_mode has been removed in PHP 5.4 so ini_get('safe_mode') returns nothing. max_execution_time works properly. Well, I'll continue to think what can I do. Thanks a lot for your help! – mu3 Apr 21 '13 at 13:07