Well basically I may want to execute a script that may take as much as 1 hours as well.
What I really want to do is Send SMS to my users using a third party API. So
You can or you can't use set_time_limit()
?
If you can.. Use it:
<?php
// Runs forever and ever...
set_time_limit(-1);
?>
A PHP script executed from the command-line or from a shell script, cron job, etc. does not have a timeout.
For CLI-invoked scripts, even if you set the PHP script's timeout dynamically with the set_time_limit() function, it has no effect.
It's not the best options to use set_time_limit(0)
, because that'd means it'll run indefinitely even if you have a bug and your script enters an infinite loop.
Instead, if you estimate each SMS is going to take 5 seconds, use this approach:
while( $there_are_more_sms_to_be_sent ){
set_time_limit(30); // enough spare time, just in case.
// Do your sending, blah blah
}
That way, the time limit will be sequentially updated to 30 seconds. Of course you might have the infinite loop problem with that single while
, but if you have other calls inside that while
that limit will prevent those calls to be to blame.
IN THE CASE YOU CAN RUN CRON JOBS
I usually have a queue, a manager and workers. Unless you can call the sms api once at the time this model can help you, and you souldn't worry about timeouts since each worker will manage it selves.
I have something like:
<?php
// PSEUDO CODE
// grab pending from queue
// <for> {
// update to running
exec("/usr/bin/php /path/to/send.php {$id} > /dev/null &");
// }
and send.php will send each sms. Right now I have this running at a rate of 300/minute since is the max frequency that you can setup on a cron job
PHP scripts running from the command line aren't affected by max_execution_time
option.
So you don't have to worry at all.
An alternative to using JavaScript is to add the Refresh
meta tag to your page:
<meta http-equiv="Refresh" content="2; url=http://yoururl/script.php&step=x" ?>
The two in content="2; url=..
tells the browser to load the url 2 second after the page has loaded.