Summary of various methods of timed PHP scheduled Tasks execution

Source: Internet
Author: User
Three ways to implement PHP timed execution
1. Scheduled Tasks for Windows
2, the Linux script program
3. Let the Web browser refresh periodically
Specific implementation
Windows Scheduled Tasks
PHP rarely run on the win server, the specific implementation is no longer delve into, see the principle of online implementation is probably write bat script, and then let the window task add to execute this bat script, for reference: http://www.jb51.net/article/29134.htm
Script implementation of Linux
Here the main use of the crontab command,
How to use:
Crontab Filecrontab [-u user] [-u user] {-l |-r |-e}
Description
Crontab is used to allow the user to execute the program at a fixed time or at a fixed interval.
Use Crontab to write shell scripts, and then let PHP call the shell, this is the use of Linux features, should not be considered the characteristics of PHP's own language
See also: http://www.jb51.net/article/29136.htm
PHP Implementation Scheduled Tasks scheduled
Using PHP to make the browser refresh requires several issues to solve
PHP script Execution time limit, default is 30m solution: Set_time_limit (), or modify php.ini settings max_execution_time time (not recommended)
If the client browser shuts down, the program may be forced to terminate, workaround: Ignore_user_abort Even if the closing page still performs normally
If the program has been executing very likely to consume a large amount of resources, the workaround uses sleep to use the program to hibernate for a while and then perform
PHP timed Execution Code:

Copy the Code code as follows:


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;//Run every 5s
Method of Death Cycle
do{
echo ' Test '. Time (). '
';
Sleep ($interval);//Wait 5s
}while (TRUE);
Method 2---Sleep timed execution
Require_once './curlclass.php ';//Introduction file
$curl = new Httpcurl ();//instantiation
$stime = $curl->getmicrotime ();
for ($i =0; $i <=10; $i + +) {
echo ' Test '. Time (). '
';
Sleep ($interval);//Wait 5s
}
Ob_flush ();
Flush ();
$etime = $curl->getmicrotime ();
Echo ';
Echo Round (($etime-stime), 4);//Program Execution time


The test found that the efficiency is not very high,


Summarize:
Personal feeling php time to perform tasks is not very efficient, it is recommended that the task of the scheduled execution of the job or to the shell to do it, compared that is the kingly.
PS: The method of the dead loop seems to be a malicious attack on the site frequently used methods

The above describes the timing of PHP timed execution of the task of a variety of methods summary, including the timing aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.