3
I need to implement a feature, in which a certain code is executed several times after a time interval.
For example, read files provided by the API every 20 minutes. I tried something like that, but I haven’t tried it yet, I just wish I had an easy and sure way to do it.
while(true)
{
// Executa o codigo aqui;
usleep(1200);
}
Does PHP have any method for scheduling tasks? What would be the mechanism to cancel the loop, if the above idea is valid?
Francisco, I don’t know enough php to answer all your questions, but I can tell you that you can use
break;
to get out of your loop.– Rubico
The usleep / Sleep function serves to delay the execution of a script and it is not recommended to put very high values, because the script will continue running until the time runs out, that is, it will continue consuming memory throughout the running time. To schedule tasks, you must use the system tools, be it Linux or Windows, both have resources for this. In the case of Linux, the feature is called cron job, in Windows it is task manager, if I’m not mistaken. Both tools will run your PHP script as scheduled, eliminating the use of Sleep. Use Sleep
– Clayderson Ferreira
only in very specific cases, such as when you want to make a late in a loop that sends emails to avoid flooding the system with a very fast execution.
– Clayderson Ferreira
I think it’s the same problem with that question: Search database table information with PHP every 10 minutes - The answers are independent of the task that will be executed
– Bacco