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 '
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