PHP timed Schedule Task Example Tutorial

Source: Internet
Author: User

Take a look at the settings for the scheduled tasks in Uchome: 1, to put all the scheduled tasks into Database 2, each time the user to operate or open the page by a sort of a scheduled task. That is, take turns to judge, the time is up, in the user process execution. Uchome's scheduled task code on the./source/function_cron.php only analyzes the Uchome code for the approximate outcome of the scheduled task.

Ignore_user_abort () I have thoroughly studied the planning task in the previous period, think that the planning task of a lot of ideas, but the most suitable for the Web or trigger-like, is similar to DZ and phpwind, it seems that most Web applications are doing so, and several other for reference 1, In order to provide a stable trigger, with crontab+wget or AB to provide timed access to 2,nohup + PHP write daemon 3, directly in PHP to write a dead loop also provides a trigger, then need to use a cache or database to help roll out the loop, There must be sleep or usleep to control the trigger frequency 4,crontab+php5,web trigger mode, which also has a difference, one is the timing trigger, a fixed period trigger, DZ and phpwind are timing trigger, I finally use the fixed cycle trigger

Own host must use Crontab to execute command line PHP is not only written in the code in the crontab will be better, if the use of loops, it is best to separate the cycle and specific processing procedures, otherwise the memory will always increase.

The PHP manual found this function-ignore_user_abort, this function can help us to implement a cron like Linux in the implementation of the planning task, the following to see how to implement. First, take a look at the PHP manual's interpretation of this function Descriptionint ignore_user_abort ([bool $setting]) sets whether a client disconnect should cause a SCR IPT to be aborted. That is, the following program executes regardless of whether the client closes the browser.

Then take a look at its parameters: Parameterssettingif not set, the function would only return the current setting. It takes a parameter to decide whether to enable Ignore_user_ The Abort function.

Then look at its return value: Return valuesreturns the previous setting, as a Boolean. Here it is said to return the previous setting, and is bool worth, after my test, this statement is not correct, the return is clearly int type.

How to use this function of PHP to achieve the planning task? Also fall with the help of another function, this function is Set_time_limit, through Set_time_limit0) can set the program run time is unlimited, PHP default run time is 30 seconds, through Set_time_ Limit (0) allows the program to execute indefinitely.

It is possible to add ignore_user_abort (1) and Set_time_limit (0) before the execution of the program, and how should the final program be written?

Example:

    1. Ignore_user_abort (); Run script in background
    2. Set_time_limit (0); Run Script Forever
    3. $interval = 30; Do every minutes ...
    4. do{
    5. $fp = fopen (' Text3.txt ', ' a ');
    6. Fwrite ($fp, ' test ');
    7. Fclose ($FP);
    8. Sleep ($interval); Wait minutes
    9. }while (TRUE);
    10. ?>
Copy Code
  • 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.