How to perform tasks regularly by PHP
This article mainly introduces how PHP performs tasks regularly, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Concrete realization
1. Windows schedule tasks
PHP seldom runs on win servers, and the specific implementation is no longer 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.
2. Script implementation of linux
The command crontab is mainly used here.
Mode of use:
Crontab filecrontab [- u user] [- u user] {- l |-r |-e}
Description:
Crontab is used to allow users to run programs at fixed times or intervals.
Use crontab to write shell scripts, and then let PHP call shell, which takes advantage of the features of linux, which should not be counted as a feature of PHP's own language.
3. PHP implements regular execution of scheduled tasks.
There are several problems that need to be solved to refresh the browser with php
PHP script execution time limit, 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 may be forced to terminate. The solution: ignore_user_abort executes normally even if the page is closed.
If the program is running all the time, it is likely to consume a lot of resources, the solution is to use sleep to use the program to sleep for a while, and then execute
Code that is timed by PHP: