Home > Backend Development > PHP Tutorial > Summary of various methods for executing planned tasks regularly in PHP_PHP Tutorial

Summary of various methods for executing planned tasks regularly in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:22:40
Original
847 people have browsed it

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:

Copy code The code is as follows:

ignore_user_abort();//Close Even if you close the browser, the PHP script can continue to execute.
set_time_limit(3000);// Through set_time_limit(0), the program can be executed without limit
$interval=5;// Run every 5 seconds

//Method 1--Infinite loop
do{
echo 'Test'.time().'
';
sleep($interval);//Wait 5s
}while(true);

//Method 2---sleep scheduled execution
require_once './curlClass.php';//Introduction file

$curl = new httpCurl();//Instantiation
$stime = $curl->getmicrotime();
for($i=0;$i<=10;$i++){

echo 'Test'.time().'
';
sleep($interval);// Wait 5s

}
ob_flush();
flush ();
$etime = $curl->getmicrotime();
echo '
';
echo round(($etime-stime),4);//Program execution time

When testing, I found that this efficiency is not very high,

QQ截图20111216110444
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324645.htmlTechArticleThree ways to implement PHP scheduled execution 1. Windows scheduled tasks 2. Linux scripts 3. Let the web Browser scheduled refresh specifically implements Windows scheduled tasks. PHP rarely runs on win servers...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template