The implementation method of PHP plan task, support Windows/linux running

Source: Internet
Author: User
Tags curl php script set time sleep time limit centos

Using PHP to refresh your browser requires several issues
1.PHP script Execution time limit, the default is 30m solution: Set_time_limit () or modify php.ini 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 even if the shutdown page is still normal execution
3. If a program has been implemented that is likely to consume a large amount of resources, the solution uses sleep using the program to hibernate for a while and then executes

The code is as follows Copy Code

<?php

Ignore_user_abort ()//Turn off the browser, the PHP script can also continue to execute.

Set_time_limit (3000);//Through Set_time_limit (0) allows the program to execute indefinitely

$interval =5;//Run every 5s

Method 1--Dead Loops

do{

echo ' Test '. Time (). ' <br/> ';

Sleep ($interval);/wait 5s

}while (TRUE);

Method 2---Sleep timed execution

Require_once './curlclass.php ';//Introduce file

$curl = new Httpcurl ();//instantiated

$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

Set scheduled Task execution PHP file in Windows

On the internet to find some of the windows to execute the task of PHP planning method, there is a very full, but unfortunately in my this unexpectedly failed. Finally have to synthesize the various methods of the school, in order to run successfully in me.

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 includerequire even if use, all have no problem
2, the new bat file, named Test.bat, the contents are as follows:

The code is as follows Copy Code
1 D:phpphp.exe-q d:websitetest.php


The corresponding directory to change their own

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, over! You can right-click the scheduled task point "run" to try

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 http://www.centos.bz/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 http://www.centos.bz/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.

The code is as follows Copy Code
*/10 * * * */usr/bin/wget-q-o temp.txt http://www.centos.bz/myscript.php

PHP Scheduled Task Introduction 2010-05-06 10:10 before the SE is particularly interested in, but I will not java,lucene and other search engine development tools, so constantly mining the effectiveness of PHP.

Finally found that PHP can also do crawl, and the principle is easy: Get the page source file directly, and then through the regular or string reference interception to obtain the required information. However, performance can not be compared with the search engine's multithreaded crawl.

After the implementation of the previous step, and thinking, if the crawl can be automatically timed to get, then manually run the executable page will be saved.

Later also in some PHP open source program to learn about the "planning task" effect: You can run a regular program, such as database backup, update the cache, generate static pages, generate site maps.

Recently because the project needs to regularly update the remote database to the local, online search, and really found.

Ignore_user_abort (), function collocation set_time_limit (0), and sleep ($interval), you can achieve the above Automatic Updates.

First, give a basic paradigm in which you have a personal test program:

The code is as follows Copy Code

<?php
Ignore_user_abort (); Run script in background
Set_time_limit (0); Run Script Forever
$interval = 30; Do every minutes ...
do{
$fp = fopen (' Text3.txt ', ' a ');
Fwrite ($fp, ' test ');
Fclose ($FP);
Sleep ($interval); Wait minutes
}while (TRUE);
?>

Run the program first, then close the page, the program is still running, test will fill every 30 seconds to the Text3.txt file.

Personally feel that PHP time to perform the task is not very efficient, suggest that the work on the timing of the task or to the shell to do it, compare that is the King

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.