Implementation of php scheduled tasks that support windows and linux

  • 2020-11-26 18:58:17
  • OfStack

This article illustrates the implementation of php scheduling tasks that support windows and linux. This includes using winodows to plan tasks under winows and using linux methods in linux. Share to everybody for everybody reference. Specific implementation methods are as follows:

Using php to refresh the browser requires several fixes:

1.PHP script execution time limit, default is 30m solution: set_time_limit(); Or modify PHP.ini to set max_execution_time time (not recommended)
2. If the client browser is closed, the program may be forced to terminate. The solution: ignore_user_abort performs normally even if the page is closed
3. If program 1 is executed directly and is likely to consume a large amount of resources, the solution is to use sleep to use program Sleep 1 will then be executed

<?php
ignore_user_abort();// Close your browser, PHP The script can also continue to execute .
set_time_limit(3000);// through set_time_limit(0) You can let the program run indefinitely
$interval=5;// every 5s run
 
// methods 1-- Infinite loop
do{
echo ' test '.time().'<br/>';
sleep($interval);// Waiting for the 5s
}while(true);
 
// methods 2---sleep Regularly perform
require_once './curlClass.php';// The introduction of the file
 
$curl = new httpCurl();// instantiation
$stime = $curl->getmicrotime();
for($i=0;$i<=10;$i++){
 
echo ' test '.time().'<br/>';
sleep($interval);// Waiting for the 5s
 
}
ob_flush();
flush();
$etime = $curl->getmicrotime();
echo '<hr>';
echo round(($etime-stime),4);// Program execution time

Set the scheduled task in WINDOWS to execute the PHP file

I found some methods of WINDOWS to carry out PHP's planning task on the Internet. One of them is very complete, but Unfortunately I failed in this task. In the end, I had to combine all the different approaches to make it work for me.

1. Write an PHP program and name it ES43en.php with the following contents:

<?php
$fp = fopen("test.txt", "a+");
 fwrite($fp, date("Y-m-d H:i:s") . " It worked! n");
 fclose($fp);
?>

Program bold write, what include, require despite using, no problem

2. Create a new Bat file and name it test.bat, as shown below:

D:phpphp.exe -q D:websitetest.php

Change the corresponding directory by yourself

3. Set up WINDOWS planning tasks:

Start � > The control panel? > Task plan? > Add task plan
Browse the folder and select the bat file above
Set time and password (login WINDOWS)
I'll just save it.

4, over! You can right-click the scheduled task and click "Run" to try it

Crontab of Linux executes the PHP script

1. Execute the script using PHP in Crontab

Just like calling normal shell script 1 in Crontab (specific Crontab usage), use the PHP program to call the PHP script.
Perform ES93en. php every 1 hour as follows:

# crontab -e 
00 * * * * /usr/local/bin/php /home/john/myscript.php

One/usr local/bin php as PHP application path.

2. Use URL execution scripts in Crontab

If your PHP script can be triggered by URL, you can use lynx or curl or wget to configure your Crontab.

The following example USES the Lynx text browser to access URL to execute the PHP script every hour. Lynx text browser opens URL by default using dialog. However, as shown below, we use the -ES124en option on the lynx command line to convert URL's output to standard output.

00 * * * * lynx -dump http://www.centos.bz/myscript.php

The following example USES CURL to access URL to execute the PHP script every 5 points. Curl displays the output in standard output by default. Using the "ES134en-ES135en" option, you can also dump the output of the script to a temporary file.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.centos.bz/myscript.php

The following example USES WGET to access URL to execute the PHP script every 10 points. The -ES143en option represents quiet mode." -ES144en ES145en.txt "means that the output will be sent to a temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.centos.bz/myscript.php


I was particularly interested in se before, but I did not know java, lucene and other search engine development tools, so I kept exploring the efficacy of php.

It turns out that php can also do fetching, and the principle is very simple: directly obtain the page source file, and then through regular or string reference interception to obtain the required information. But the performance can not compare with the multi-threaded search engine fetching.

After implementing the previous step, I thought, if fetching can be automatically timed, then manually running executable pages would be saved.

Later, I also learned about the effect of "planning tasks" in some php open source programs: You can run certain programs regularly, such as database backup, update cache, generate static pages, generate website map, etc.

Recently, due to the project needs to update the remote database regularly to the local, the Internet search, really found.

ignore_user_abort(); The function matches set_time_limit(0); And sleep ($interval); Can realize the above automatic update.
A basic paradigm is presented, in which an individual test program is:

<?php
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=30; // do every 15 minutes...
do{
$fp = fopen('text3.txt','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 test fills in the text3.txt file every 30 seconds.

Personally, I think the efficiency of PHP's regular task execution is not very high. I suggest that shell should do the work of regular task execution, and that comparison is the king.

Hopefully this article has been helpful in your C# programming.


Related articles: