Search code examples
phpapicrondelay

should we use cron if we are using delay in our program...?


So i have written a program to call API from a website each 20 minutes. i"ve done this by giving the sleep() function in php. i have given this delay inside a while loop. how can i execute the same function using cron? this is the while loop..

<?php
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1200);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',0);
//code;
while($r=mysql_fetch_array($res))
 {

    //code;
    if(sleep(1200)!=0)
    {
        echo "sleep failed script terminating"; 
        break;
    }
    flush();
    ob_flush();
}
?>

Solution

  • Ditch the sleep, and use the cron.

    • In your console type crontab -e
    • Set up the following;

    */20 * * * * php -f path/to/script.php