Explain how PHP executes scheduled tasks ,. PHP is relatively weak in executing scheduled tasks in PHP. if you only use php, you can do the following: phpignore_user_abort (); after closing the browser, continue to execute php code details PHP to execute scheduled tasks,
PHP is relatively weak in this regard. if you only use php, you can implement the following:
<? Php ignore_user_abort (); // after closing the browser, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 1; // How long do {$ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com \ n "); fclose ($ fp); sleep ($ sleep_time);} while (true);?>
But when I execute the script, even if I close the browser, I cannot stop this program at all, so you need a script execution switch, you can use the method introduced by external files to implement the include switch variable during the while loop. Then we can achieve this:
Create an external variable file switch. php with the following content:
<? Phpreturn 1; // 1 execution script 0 exit execution script?>
The script for improvement is as follows:
<? Php ignore_user_abort (); // after the browser is closed, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 5; // How long does it take to execute $ switch = include 'switch. php '; while ($ switch) {$ switch = include' switch. php'; $ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com $ switch \ n"); fclose ($ fp ); sleep ($ sleep_time);} exit ();?>
This script is only feasible for testing and should be less efficient. for lamp, you can use crontab.
Add a small piece of code:
Ignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program $ interval = 60*30; // run do every half an hour {// Here is the code you want to execute sleep ($ interval); // wait 5 minutes} while (true );
So many articles about the implementation of PHP scheduled task execution will be introduced to you later. do not miss out.
Articles you may be interested in:
- PHP obtains the timestamp of a certain time interval.
- Linux uses crontab to implement scheduled PHP execution tasks
- Example of cron scheduled task executor in php
- Use the sleep function in PHP to share scheduled task instances
- Implementation of PHP scheduled task delay
- Debug related PHP script examples of scheduled tasks in WordPress
Ghost, PHP should be said to be relatively weak in this regard, if you only use php to implement the following: php ignore_user_abort (); // close the browser, continue to execute php code...