We introduced in the previous article on the implementation of the PHP program task principle, I believe that the small partners understand the principle of PHP planning tasks, then we continue today to introduce you how PHP implementation of scheduled Tasks!
One, Windows Scheduled tasks
1, write a PHP program, named test.php, the content is as follows
<? $fp = fopen ("Test.txt", "A +"); Fwrite ($fp, Date ("Y-m-d h:i:s"). "Success has been successful!" \ n "); Fclose ($FP); ? >
Program boldly write, what include\require, even if used, no problem
2. Create a new bat file named Test.bat, which reads as follows:
D:\php\php.exe-q D:\website\test.php
3. Set up Windows Scheduled tasks:
Start –> Control Panel –> Task Scheduler –> add a task schedule
Browse for Folder Select the bat file above
Set time and password (login to Windows)
Save it.
4, over! You can right-click to schedule the task point "run" to try
Second, the Linux script implementation
First, use PHP to execute script in crontab
Just like calling a normal shell script in crontab (specific crontab usage), use a PHP program to invoke the PHP script.
Each hour executes myscript.php as follows:
# crontab-e XX * * * * /usr/local/bin/php/home/john/myscript.php
/usr/local/bin/php is the path to the PHP program.
Second, use URL to execute script in crontab
If your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.
The following example uses the Lynx text browser to access the URL to execute PHP scripts every hour. The Lynx text browser opens the URL by default using dialog. However, like the following, we use the-dump option in the Lynx command line to convert the output of the URL to standard output.
XX * * * * lynx-dump http://www.centos.bz/myscript.php
The following example uses Curl to access the URL to execute PHP scripts every 5 minutes. Curl defaults to show output in standard output. With the "curl-o" 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 the wget access URL to execute PHP scripts every 10 minutes. The-q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file
*/10 * * * */usr/bin/wget-q-o temp.txt http://www.centos.bz/myscript.php
Third, PHP implementation of scheduled Tasks
Using PHP to make the browser refresh requires several issues to solve
1.PHP script Execution time limit, default is 30m workaround: Set_time_limit (), or modify php.ini setting Max_execution_time time (not recommended)
2. If the client browser is closed, the program may be forced to terminate, workaround: Ignore_user_abort Even if the closing page still executes normally
3. If the program has been executing very likely to consume a large amount of resources, the solution uses sleep to use the program to sleep for a while and then performs
Code for PHP timed execution
<?php Ignore_user_abort ();//switch off the browser, PHP script can continue to execute. Set_time_limit (3000);//Set_time_limit (0) allows the program to execute indefinitely $interval =5;//every 5s operation //method of the dead loop do{ echo ' Test '. Time (). ' <br/> '; Sleep ($interval);//wait 5s }while (true); Method 2---Sleep timed execution of require_once './curlclass.php ';//introduction file $curl = new Httpcurl ();//instantiation $stime = $curl- >getmicrotime (); for ($i =0; $i <=10; $i + +) { echo ' Test '. Time (). ' <br/> '; Sleep ($interval);//wait 5s } Ob_flush (); Flush (); $etime = $curl->getmicrotime (); Echo '
When I tested it, I found it wasn't very efficient.
Summarize:
This article through the Windows Scheduled task, the Linux script implementation, as well as PHP to perform scheduled tasks of the instance, let the small partners more directly understand PHP scheduled execution of tasks, I hope that you help!
Related recommendations:
Analysis of the principle of PHP implementation planning task
PHP implementation of scheduled tasks and continuous process instances Fsockopen
PHP Scheduled task detects user connection status