You have two possibilities using linux CRON or crafting a script that runs indefinitely.
Run script forever
To run the script indefinitely just do
set_time_limit(0);
while (true) {
// código a ser executado de tempo em tempo
if ( $quit === true ) {
break;
}
sleep(300);
}
Note that I put a stop condition, making it possible to interrupt the execution.
That’s the worst way to do it for a lot of reasons. PHP is not meant to run endlessly, as memory usage can increase considerably as running time increases. Another drawback is maintenance, as the script may stop and you will not know. Use this method only in the last case.
Scheduling using linux CRON
The other most recommended way is to use CRON or the windows task scheduler.
This way is the most used, because the script runs from time to time and even if an error occurs, the script will run again at the programmed time.
To add your task and schedule the execution, log in to the server terminal and type:
sudo crontab -e
It will open a text file where you can place the scheduled tasks following some patterns. In your case, the line to be added would look like this:
*/5 * * * * php ~/caminho/para/o/script.php
If you want to call a specific URL, you will have to use the wget command on the task so:
*/5 * * * * wget -O - http://www.exemplo.com.br/script.php > /caminho/do/arquivo/para/armazenar/a/saida.txt
To learn more about CRON, read this question:
Set up Cronjob to run every 5 minutes, when it’s between 5 to 20 hours
If you want to better control your task scheduling, I wrote an answer explaining how to create a tool by PHP to manage the executions.
/a/103262/3938
Could you give more details ? Put the code you already tried in the question.. Do this only with PHP ? Will execute the PHP how ? Calls via Ajax ? Cron ? Without details it’s hard to help.
– NoobSaibot
I made an edit and put the code I started doing by php it uses the database to add the information
– Psicodelico ॐ
That verification
$row['tempo'] <= $row['tempo']
is wrong, how can time be less or equal to time ? The librarymysql_*
has been discontinued, its use is not recommended, read Why should we not use mysql type functions_*?.– NoobSaibot