How to implement PHP scheduled tasks

Source: Internet
Author: User
Recently, because the project needs to do some processing on the remote database on a regular basis, there is a need to make a plan for what the task looks like.

After the effort, finally using PHP to achieve this scheduled task, the following share the implementation process. The use of the PHP implementation of the planning task is mainly used

Ignore_user_abort () set_time_limit (0) sleep () these three functions.

The specific code is as follows:

Ignore_user_abort ();//The function is that when the user shuts down the browser, the 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 (' www.phpernote.com ');//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

In the specific implementation process of the individual feel PHP time to perform tasks is not very efficient, it is recommended that the task of timing tasks or to the shell to do it, relatively speaking, this method is too reluctant to achieve, and the shell is a professional level.

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