Php simulates linux crontab to implement scheduled tasks
This article describes how to implement scheduled tasks by simulating linux crontab in php.
PHP scheduled tasks require two files.
1. crontab. conf. php (configuration file)
<? Php // when the value is 0, close the scheduled task return 1;
2. crontab. php (subject file)
<? Phpignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // you can use set_time_limit (0) to run the program without restrictions $ interval = 60*3; // run the program every 3 minutes $ I = 0; do {$ nowTime = date ("Y-m-d H: m: s"); $ run = include 'crontab. conf. php'; if (! $ Run) {file_put_contents ("crontab_task.log", "= scheduled task ended $ nowTime = \ r \ n", FILE_APPEND); die ('job has ended. ');} // put the code to be executed here sleep ($ interval); // wait for 3 minutes $ data = "$ I> the scheduled task is running... (running status: $ run) $ nowTime \ r \ n "; file_put_contents (" tasktest.txt ", $ data, FILE_APPEND); $ I ++;} while (true );
Mainly change the return 0 in crontab. conf. php to close the scheduled task.
Note: When the php service is turned off, the scheduled task script will also be stopped and will not be restarted automatically.