Home >Backend Development >PHP Tutorial >php scheduled tasks
(1) The program only needs to be started once after setting the scheduled tasks, and then the program will continue to run until the server is restarted
(2) If it is run repeatedly, it may not be the result you want, in which case multiple same scheduled tasks will be started
(3) If every page includes this page, multiple permanently running programs will be started, which will consume unnecessary system resources.
It will also affect the normal access of the page. If it is placed in front, the page will always be accessed. In the waiting return state (that is an infinite loop)
ignore_user_abort(true); //Even if the Client is disconnected (such as closing the browser), the PHP script can continue to execute.
set_time_limit(0); / / The execution time is unlimited. The default execution time of PHP is 30 seconds. Through set_time_limit(0), the program can be executed without limit
$interval=60*5; // Run every 5 minutes
$f = 'lock .txt';
if(file_exists($f)){ //Determine whether the marked file exists, and exit if it exists to prevent repeated runs
exit();
}
do{
if(@get_file_contents($f) == 'stop'){ //Set the stop condition. When stopping, just write stop
to lock.txt Scheduled tasks
....omit task code
sleep($interval);//The program pauses for 5 minutes
}while(true);
@unlock($f); //Delete marked files
?>
This is just a way to use code. Another way is to use Php cli mode to implement scheduled tasks
For example, use run->cmd
under window to run php.exe, and then press AT, according to the requirements inside Just enter it
Different systems have different methods, and Linux is different. It has not been implemented specifically, but it is definitely possible