Three methods for PHP scheduled task execution are described in detail ,. Three methods for PHP scheduled task execution are described in detail, PHP scheduled execution in three ways: 1. windows scheduled task 2. linux script program 3. make the web browser regularly refresh the specific implementation. 1. explain the three methods for PHP scheduled task execution,
PHP scheduled execution in three ways
- 1. windows scheduled tasks
- 2. linux script program
- 3. regularly refresh web browsers
Implementation
1. scheduled tasks in windows
PHP rarely runs on the win server, and the specific implementation is not further explored. The principle of online implementation is probably to write the bat script, and then let the window task add and execute the bat script.
2. linux script implementation
The crontab command is used here,
Usage:
The code is as follows: crontab filecrontab [-u user] [-u user] {-l |-r |-e}
Note:
Crontab is used to allow users to run programs at a fixed time or interval.
Use crontab to write the shell script and then let PHP call the shell. this is based on the linux feature and should not be a feature of the PHP language.
3. implement scheduled tasks in PHP
Several problems need to be solved to refresh the browser using php
PHP script execution time limit, the default solution is 30 m: set_time_limit (); or modify PHP. ini to set max_execution_time (not recommended)
If the browser of the client is closed, the program may be terminated. Solution: ignore_user_abort can still run normally even if the page is closed.
If the program runs continuously, it may consume a lot of resources. the solution is to use sleep to sleep the program for a while, and then run
PHP code regularly executed:
<? Phpignore_user_abort (); // close the browser and run the PHP script. set_time_limit (3000); // use set_time_limit (0) to allow unlimited execution of the program $ interval = 5; // run every 5s // method 1 -- Endless loop do {echo 'test '. time ().'
'; Sleep ($ interval); // wait for 5 s} while (true); // method 2 --- sleep regularly executes require_once '. /curlClass. php '; // introduce the file $ curl = new httpCurl (); // instantiate $ stime = $ curl-> getmicrotime (); for ($ I = 0; $ I <= 10; $ I ++) {echo 'test '. time ().'
'; Sleep ($ interval); // wait for 5 s} ob_flush (); flush (); $ etime = $ curl-> getmicrotime (); echo ''; echo round ($ etime-stime), 4); // program execution time
During the test, we found that the efficiency was not very high.
Summary:
I personally feel that the efficiency of PHP scheduled task execution is not very high. it is recommended that you hand over the scheduled task execution to shell.
The above are three methods for PHP to regularly execute tasks. we also briefly introduce the crontab command mentioned at the end of the previous article, and hope you will get some gains.
Articles you may be interested in:
- PHP implementation code for scheduled and scheduled tasks
- Summary of various methods for PHP scheduled execution of scheduled tasks
- Implementation principle of scheduled tasks in php
- Implementation of php scheduled tasks
- Linux uses crontab to implement scheduled PHP execution tasks
- PHP implementation of scheduled task execution
Scheduled, PHP scheduled execution in three ways: 1. windows scheduled task 2. linux script program 3. make the web browser regularly refresh implementation 1 ,...