3

I have made a PHP script which probably would take about 3 hours to complete. I run it from browser and after about 45minutes it stops doing anything. I know this since its polling certain web addresses and then saves some data to database. So it basically stops putting any data to database which lead me to conclusion that it has stopped. It still shows in browser like it would be loading the page though but its neverending.

There arent any errors so it probably is some kind of timeout... But where it occurs is mystery or how can I prevent it from happening. In my case I cant use the CLI, I must user browser client to initiate the script.

I have tried to put

set_time_limit(0);  

But it had no apparent effect. Any suggestions what could cause the timeout and a fix for it?

2
  • My suggestion is that you put some metrics (i.e. output to screen) into your script such that you can see where it is failing. I would also suggest checking error logs for any errors yo might be getting(you are logging errors aren't you)? Commented Jan 18, 2013 at 21:40
  • 1
    @Mike It just stops. No errors. Commented Jan 18, 2013 at 22:24

5 Answers 5

5

Try this:

set_time_limit(0); 
ignore_user_abort(true);
ini_set('max_execution_time', 0);
Sign up to request clarification or add additional context in comments.

1 Comment

max_execution_time set to 0 is a very bad idea, because if the script is in a infinite loop, then it will not break until you kill the process or restart the webserver.
3

Most webhosts kill processes that run for a certain length of time. This is intended as a failsafe against infinite loops.

Ask your host about this and see if there's any way it can be disabled for this particular script of yours. In some cases, the killer doesn't apply to Cron tasks, or processes run by SSH. However, this varies from host to host.

3 Comments

I would have to be very specific what to ask, your suggestion was too vague, are you able to give me some more information about how that disabling can be done ?
@Pehmolelu How about "I'm trying to run mypage.php for 3 hours straight, but it stops after 45 minutes. Why?" then wait for them to stop laughing at you and they'll start suggesting other ways to do it properly.
@Sammitch I know the best way would probably be doing it from CLI but thats not an option for me. I also have to be able to just initiate it, not be required to watch the screen for hours. Im mainly looking for some hints what could cause this, and Kolinks answer is one, I just dont know yet how to start investigating it deeper since that is not something I have skills for, yet.
0

Might be the browser that's timing out, not sure if browsers do that or not, but then I've also never had a page require so much time.

Suggestion: I'm assuming you are running a loop. Load a page then run each iteration of the loop in an ajax call to another page, not firing the next iteration until the previous one returns.

3 Comments

That would do it but it would then require me to have the page actually open whole time. Now I can just initate it and if I want I could close the window and script would still run.
true, but would also allow you to build a progress bar in case you wanted to know how far along the process is.
Yeah for that I have used flush() to throw some messages
0

There's a setting in PHP to kill processes after sometime. This is especially important for shared servers (you do not want that one process slows up the whole server).

You need to ask your host if you can make modifications to php.ini (through .htaccess). In particular the max_execution_time setting.

3 Comments

as I said, I have tried using set_time_limit(0) which should modify that setting to be infinite.
And you're sure your host has not blocked that function? stackoverflow.com/questions/11999678/php-timeout-set-time-limit0-dont-work
I'm sure it works, I have checked it with echo ini_get('max_execution_time'); Thanks for trying to help anyways :)
0

If you are using session, then you would need to look at 'session.cookie_lifetime' and not set_time_limit. If you are using an array, the array size might also fill up.

Without more info on how your script handles the task, it would be difficult to identify.

1 Comment

Im not using session, the thing is just behind htaccess. Array size fill up? Never heard of that but no, I just get the data from page and then save the data and move along to the next page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.