Home > Article > Backend Development > Summary of various methods for executing planned tasks regularly in PHP_PHP Tutorial
Three ways to implement PHP scheduled execution
1. Windows scheduled tasks
2. Linux scripts
3. Let the web browser refresh regularly
Specific implementation
Windows scheduled tasks
PHP rarely runs on win servers, and the specific implementation will not be delved into. The principle of online implementation is probably to write a bat script, and then let the window task add and execute this bat script, for details, please refer to: http://www.jb51.net/article/29134.htm
Linux script implementation
The crontab command is mainly used here,
Usage:
crontab filecrontab [ -u user ] [ -u user ] { -l | -r | -e }
Instructions:
Crontab is used to allow users to execute programs at a fixed time or at fixed intervals
Use crontab to write shell scripts, and then let PHP call the shell. This takes advantage of the characteristics of Linux and should not be considered PHP's own language. Features
can be found at: http://www.jb51.net/article/29136.htm
PHP implements scheduled execution of scheduled tasks
Use php to allow browsing Several problems need to be solved for browser refresh
PHP script execution time limit, the default is 30m Solution: set_time_limit(); or modify PHP.ini to set max_execution_time time (not recommended)
If the client browser is closed, the program It may be forced to terminate. Solution: ignore_user_abort will still execute normally even if the page is closed
If the program continues to execute, it is likely to consume a lot of resources. The solution is to use sleep to sleep the program for a while, and then execute
PHP timing Executed code:
When testing, I found that this efficiency is not very high,
Summary:
Personally, I feel that the efficiency of PHP's scheduled task execution is not very high. It is recommended that the work of scheduled task execution be left to the shell. Comparison is the best way.
ps: The endless loop method seems to be a method often used by malicious attacks on websites