5

Currently I have a problem with GoDaddy and its hosting. The maximum amount of time allowed for a PHP script to run is set to 120 seconds = 2 mins.

Sometimes when I run my script, it takes more than 120 seconds and sometimes it takes 30 seconds.

When it takes over 120 seconds, I get a Internal Server Error (500).

The question I have is, is it possible to figure out if a script run time is at 110 seconds and then refresh the page so that the Internal Server Error does not occur.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Sohel Mansuri
  • 564
  • 2
  • 6
  • 18
  • 1
    Can't you use `set_time_limit()`? – kero Apr 02 '13 at 00:34
  • You might be able to do something with `microtime();` but as for the refreshing I dont have any idea. – Lemon Drop Apr 02 '13 at 00:36
  • @kingkero, that would just set the time limit. I need to know if the time limit is reached and if it is, I need to refresh the page. – Sohel Mansuri Apr 02 '13 at 00:36
  • http://stackoverflow.com/questions/6486364/is-there-a-set-time-out-equivalent-in-php – d-_-b Apr 02 '13 at 00:37
  • @iight, Sorry but this is not what I am looking for. My dilemma is that a script runs but sometimes it takes longer than 120 seconds. I want to keep track of how long a script is running before it is done loading... – Sohel Mansuri Apr 02 '13 at 00:42
  • Then possible duplicate of http://stackoverflow.com/questions/535020/tracking-the-script-execution-time-in-php – kero Apr 02 '13 at 00:44
  • @kingkero, sorry again. Let me explain one more time: Is there a way to track the time it takes for a script to run and if it hits 120 seconds before finishing, then refresh it... – Sohel Mansuri Apr 02 '13 at 00:48
  • @sohel. You need to show us what the script does and how it is doing it. If not, all you are going to get will be general generic answers. – iWantSimpleLife Apr 02 '13 at 00:51
  • 1
    no you can't periodically check in on your script like setInterval with javascript. PHP is a single-threaded. – d-_-b Apr 02 '13 at 00:53

1 Answers1

2

If what ever is causing your script to run long is some sort of self contained blocking function (like a database query), then you are kind of screwed.

If not, say you are in a very large loop, then just do the math your self.

At the very start of your script, save the current time: $t_start = time();

Then at the top of your loop, do a $runtime = time() - $t_start; then you have how long you have been running, and can use that to break your loop gracefully if nearing your 120 limit.

Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50