0

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.

Edited by mikeflash: n/a

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by maba001
0

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.

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.