How to make a schedule counting from time to time?

Asked

Viewed 515 times

1

Good guys would like to know how to create a simple schedule, example I put a script to run every 5 minutes, by logic it runs the first time and wait another 5 minutes to run again skipping 5 min tried to do but it didn’t work very well. Any help is welcome

if(time() - $row['time'] == $row['tempo'] + $row['agenda'] && $row['agenda'] >= 30){

        if($row['tempo'] <= $row['tempo'] + $row['agenda']){
            mysql_query("UPDATE `agendamento` SET `tempo` = tempo+".$row['agenda'].", `tempo_anterior` = tempo_anterior+".$row['agenda']." WHERE `id` =".$row['id'].";"); //ADICIONA + SEGUNDOS NO BANCO DE DADOS
        }

}else if($row['tempo'] > $row['agenda'] && $row['agenda'] >= 30){

    // EXECUTA MINHA FUNÇÃO SE CONTAR NO TEMPO CERTO

}
  • 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.

  • I made an edit and put the code I started doing by php it uses the database to add the information

  • That verification $row['tempo'] <= $row['tempo'] is wrong, how can time be less or equal to time ? The library mysql_* has been discontinued, its use is not recommended, read Why should we not use mysql type functions_*?.

1 answer

2

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.