954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Script ignoring time limit?

Hey all,

I have several cron jobs running but some of them take a lot of time to complete, since it requires fetching data from an external website and then updates my site's database. My hosting provider has a 120 second time limit for PHP scripts, which is too low for my script, the reason why I run that script in blocks, I mean, I update as much as I can in 120 seconds and then, 3 minutes later, the next cron task picks up where last cron left off, and so on until it completes.

Additionally, I edited my script so that it would insert a new "activity feed" in a DB table when it was completed. The script measures time from the start to the very end and includes that in the mysql statement. (to clarify, it measures time for the current script, not cumulative with the previous one)

Now, checking the activity feed after the script ran, I've noticed that sometimes the script runs for more than 1900 seconds! That's over half an hour! I don't think i'll even take that much time to update all my database with the external site info, but since it's done record by record, one at a time (over 1200 rows) who knows. Anyway, I'd like my script to run for 120 seconds, get shut down by the time limit, then wait 3 minutes for the next script to pick up where the last one left off, but this is not happening. Instead, I get all of the cron tasks run for their full duration ignoring the time limit, and I haven't used set_time_limit(0); in my code, which would be my first clue.

I re-checked my host's php.ini and yes, the time limit is set to 120.

Any ideas why this is happening? I don't want to get in trouble with my hosting provider, I'm sure that as soon as they notice this, they'll warn me or worse. Is there any way I can make sure that the script doesn't run for more than 120 seconds? I trying setting time limit myself to 120 but no luck.

mikeflash
Newbie Poster
12 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

In case you are looping, you could just do the following:

$starttime = time();
...
foreach (....) { // your loop
    // your active code here
    .... 
    if (time() - $starttime) > 120) break;
}


Would be the simplistic approach.

maba001
Junior Poster in Training
60 posts since Jun 2008
Reputation Points: 9
Solved Threads: 9
 

Well, that could work, since there's indeed a loop involved. Thanks.

But, any ideas why would the script ignore the time limit set in php.ini? It doesn't make sense.

mikeflash
Newbie Poster
12 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

Hello again,

it might be that the set_time_limit(0) call gets honored. This would explain why it runs that long. Actually I don't know how reliable the time limit really is. I have seen similar behavior but only in cases where I do system calls to external helpers. In such a case if the external program does not return, the server might kill the connection (the httpd that is) but the PHP script itself still runs. So you won't see the timeout. But -- not really sure, this is just something I have seen happening.

maba001
Junior Poster in Training
60 posts since Jun 2008
Reputation Points: 9
Solved Threads: 9
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: