This article describes the implementation method of PHP planning tasks that support Windows and Linux. It includes using winodows to plan tasks under winows, and also using linux methods in linux. Share it for your reference. The specific implementation method is as follows:
Using php to refresh the browser requires several problems:
The script execution time limit is 30m by default. Solution: set_time_limit(); or modify to set max_execution_time time (not recommended)
2. If the client browser is closed, the program may be forced to terminate. Solution: ignore_user_abort will still execute normally even if the page is closed.
3. If the program is executed continuously, it is likely to consume a lot of resources. The solution is to use sleep to sleep and sleep for a while, and then execute it.
ignore_user_abort();// Turn off the browser, and the PHP script can continue to be executed.
set_time_limit(3000);// Set_time_limit(0) can enable the program to be executed without restrictions
$interval=5;// Run every 5 seconds
//Method 1--Dead Loop
do{
echo 'test'.time().'<br/>';
sleep($interval);// Wait for 5 seconds
}while(true);
//Method 2---sleep timed execution
require_once './';//Introduce files
$curl = new httpCurl();//Instantiation
$stime = $curl->getmicrotime();
for($i=0;$i<=10;$i++){
echo 'test'.time().'<br/>';
sleep($interval);// Wait for 5 seconds
}
ob_flush();
flush();
$etime = $curl->getmicrotime();
echo '<hr>';
echo round(($etime-stime),4);//Program execution time
Setting up scheduled tasks in WINDOWS to execute PHP files
I found some methods for WINDOWS to execute PHP planning tasks online. One of them was written in a very complete way, but unfortunately I didn't pass it. In the end, I had to combine the methods of various sects to successfully operate in my life.
1. Write a PHP program named as follows:
$fp = fopen("", "a+");
fwrite($fp, date("Y-m-d H:i:s") . " Successfully successful! n");
fclose($fp);
?>
The program is bold and it is fine to use include or require.
2. Create a new Bat file, name it, the content is as follows:
Change the corresponding directory yourself
3. Establish WINDOWS planning tasks:
Start –> Control Panel –> Task Schedule –> Add Task Schedule
Browse the folder and select the above bat file
Set time and password (login to WINDOWS)
Just save it.
4. Over! You can right-click to plan the task point "Run" to try
Crontab of Linux executes PHP scripts
1. Use PHP to execute scripts in Crontab
Just like calling a normal shell script in Crontab (specific Crontab usage), use a PHP program to call a PHP script.
Each hour is executed as follows:
00 * * * * /usr/local/bin/php /home/john/
where /usr/local/bin/php is the path of the PHP program.
2. Use URL to execute scripts in Crontab
If your PHP script can be triggered via URL, you can configure your Crontab using lynx or curl or wget.
The following example is to use the Lynx text browser to access the URL to execute PHP scripts every hour. The Lynx text browser uses dialogue to open the URL by default. However, like below, we use the -dump option in the lynx command line to convert the output of the URL to standard output.
The following example is to use CURL to access the URL to execute PHP scripts every 5 minutes. Curl displays output in standard output by default. Using the "curl -o" option, you can also dump the output of the script to a temporary file.
The following example is to use WGET access URL to execute PHP scripts every 10 minutes. The -q option indicates quiet mode. "-O" means that the output will be sent to a temporary file.
I was particularly interested in SE before, but I didn’t know how to develop search engines such as Java and Lucene, so I kept digging into the effects of php.
Finally, I found that php can also be used for crawling, and the principle is very easy: directly obtain the page source file, and then obtain the required information through reference interception of regular or strings. However, performance cannot be compared with search engine multi-threaded crawling.
After implementing the previous step, I thought again that if the crawl can be automatically obtained at a time, then manually running the executable page will be saved.
Later, I also learned about the effect of "planned tasks" in some php open source programs: a program can be run regularly, such as database backup, update cache, generate static pages, generate website maps, etc.
Recently, since the project needs to regularly update the remote database to the local area, I searched online and found it.
The ignore_user_abort(); function can be combined with set_time_limit(0); and sleep($interval); to achieve the above automatic update.
First, give a basic paradigm, which contains a personal test procedure:
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=30; // do every 15 minutes...
do{
$fp = fopen('','a');
fwrite($fp,'test');
fclose($fp);
sleep($interval); // wait 15 minutes
}while(true);
?>
First run the program, then close the page. The program is still running, and the test will fill the file every 30 seconds.
I personally feel that the efficiency of PHP's scheduled tasks is not very high. It is recommended that the work of scheduled tasks should be left to the shell. Comparison is the king.
I hope this article will be helpful to everyone's C# programming.