Summary of various methods for timing PHP to execute scheduled tasks on a scheduled basis

WBOY
Release: 2016-07-29 08:47:26
Original
990 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, specific implementation I won’t go into details anymore. The principle of online implementation is probably to write a bat script, and then let the window task add and execute the bat script. For details, you can refer to: http://www.jb51.net/article/29134.htm
linux script implementation
The crontab command is mainly used here.
Usage method:
crontab filecrontab [ -u user ] [ -u user ] { -l | -r | -e }
Explanation:
crontab is used to allow the user to set the time at a fixed time Or for executing programs at fixed intervals
Use crontab to write a shell script, and then let PHP call the shell. This is using the characteristics of Linux and should not be considered the characteristics of PHP's own language. You can refer to: http://www.jb51.net/ article/29136.htm
PHP implements scheduled execution of scheduled tasks
Using PHP to refresh the browser requires solving several problems
PHP script execution time limit, the default is 30m Solution: set_time_limit(); or modify PHP.ini to set the max_execution_time time (Not recommended)
If the client browser is closed, the program may be forced to terminate. The solution: ignore_user_abort will still execute normally even if the page is closed.
If the program keeps executing, it is likely to consume a lot of resources. The solution is to use sleep to use the program to hibernate. After a while, then execute the
PHP scheduled execution code:

Copy the code The code is as follows:

ignore_user_abort();//Close the browser, and 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 5s
//Method 1--Infinite loop
do{
echo 'Test' .time().'
';
sleep($interval);//Wait for 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,

 PHP定时执行计划任务的多种方法小结Summary:
Personally, I feel that PHP’s timed execution of tasks is not very efficient. It is recommended that the timed execution of tasks should 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

The above has introduced a summary of various methods for timing PHP to execute scheduled tasks on a scheduled basis, including timing aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!