0

I have an issue whereby occasionally a script that I run (which has dependencies on other websites) hangs trying to retrieve elements from these other websites if there is no response. I appreciate that I need to look at this part of the code again.

But, in the meantime, I want to understand why these commands that I have tried and put at the top of my PHP code are not working?

ini_set('max_execution_time', 30);
set_time_limit(30);

The scripts are run entirely in PHP and executed as CLI PHP (not as web pages launched from a browser) as :

php /var/www/html/client/index.php

The scripts execute fine, but if one of them hangs, it gets stuck as a process (as viewed via TOP or HTOP) and uses resources while it tries to execute and never closes.

Is there any other way to force the scripts to stop and close other than what I have already tried?

Thank you.

omega1
  • 1,031
  • 4
  • 21
  • 41
  • 1
    Blocking calls, eg. to Database or external sources, does not count towards execution time. You would be better off setting sensible timeouts for your external requests. – Niet the Dark Absol Feb 26 '17 at 19:20
  • Possible duplicate: http://stackoverflow.com/questions/10587323/timeout-a-function-in-php#10587359 – avysk Feb 26 '17 at 19:22

1 Answers1

0

You are using set_time_limit(30); which increases the time the script is executed. Read its documentation here.

If you have that line into a loop your are adding time to the script execution time limit on every pass.

CLI has a default time limit set of 0, which is infinite. Use the same method, but just at the beginning of the script and make sure isn't inside any loop or executed multiple times by recursion, etc.

This should fix the issue.

Pablo Ezequiel Leone
  • 1,337
  • 17
  • 22
  • Hello, Thank you for your response. I have that line at the top of the page so it only executes once and not as part of a loop. Unless I misunderstood and you mean that every time any script (where that line is present) it increases the time for ALL other scripts also? I'll check the doco now, thank you. – omega1 May 04 '17 at 13:48