Set scheduled Task execution PHP file in Windows
1, write a PHP program, named test.php, the content is as follows:
The code is as follows |
Copy Code |
? $fp = fopen ("Test.txt", "A +"); Fwrite ($fp, Date ("Y-m-d h:i:s"). "Success!" he said. n "); Fclose ($FP); ?>
|
The program boldly writes, what include/require even if use, all have no problem
2, the new bat file, named Test.bat, the contents are as follows:
D:phpphp.exe-q d:websitetest.php
3. Set up Windows Planning task:
Start –> Control Panel –> Task Schedule –> Add Task Schedule
Browse folder Select the bat file above
Set time and password (log on to Windows)
You can save it.
4, Done! You can right-click the scheduled task point "run" to try
Implementing timed tasks with PHP scripts
PHP Script Execution time limit, the default is 30m solution: Set_time_limit () or modify php.ini set Max_execution_time time (not recommended)
If the client browser is closed, the program may be forced to terminate. Solution: Ignore_user_abort Even if the page is closed still normal
If the program has been executing very likely to consume a lot of resources, the solution uses the sleep program to hibernate for a while, and then in the execution
PHP timed code:
code is as follows |
copy code |
<?php Ignore_user_abort ();//Turn off the browser and the PHP script can continue. Set_time_limit ( 3000)//through Set_time_limit (0) allows the program to execute indefinitely $interval =5;//every 5s run //Method 1--dead Loop do{ Echo ' test '. Time (). ' <br/> '; Sleep ($interval);//wait 5s }while (true); //Method 2---Sleep performs require_once './curlclass.php ';//Introducing 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 ' Echo Round (($etime-stime), 4)//program execution time |
Linux crontab execute PHP script
First, use PHP to execute scripts in crontab
Just like calling a normal shell script in crontab (specifically crontab usage), use a PHP program to invoke the PHP script.
The following myscript.php are performed every hour:
The code is as follows |
Copy Code |
# CRONTAB-E * * * * * */usr/local/bin/php/home/john/myscript.php |
/usr/local/bin/php is the path to the PHP program.
Ii. using URLs to execute scripts 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 a Lynx text browser to access URLs to execute PHP scripts every hour. The Lynx text browser opens the URL by default using the dialog method. However, like the following, we use the-dump option in the Lynx command line to convert the URL output to standard output.
The code is as follows |
Copy Code |
* * * * * * lynx-dump/myscript.php
|
The following example uses the Curl access URL to execute a PHP script every 5 minutes. Curl defaults to display output in standard output. With the "curl-o" option, you can also dump the output of the script to a temporary file.
The code is as follows |
Copy Code |
*/5 * * * */usr/bin/curl-o temp.txt/myscript.php |
The following example uses the wget access URL to execute a PHP script every 10 minutes. The-q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file.
code is as follows |
copy code |
&NBSP;*/10 * * * */usr/bin/wget-q-o temp.txt/myscript.php |