Php scheduled task instance tutorial

Source: Internet
Author: User
Tags php write
Php scheduled task instance tutorial

Let's take a look at how to set scheduled tasks in uchome: 1. store all scheduled tasks in database 2 and execute a scheduled task in sequence each time you perform operations or open the page. that is, when the time is reached, it will be executed in the user process. the uchome scheduled task code is in. /source/function_cron.php only analyzes the uchome code's rough results about the scheduled task.

Ignore_user_abort, it seems that most web applications do the same. Besides, there are several other types for reference 1. to provide stable triggers, use crontab + wget or AB to provide regular access 2, nohup + php write daemon 3, directly using php to write an endless loop and trigger it. in this case, you need to use the cache or database to assist in launching the loop, also, you must use sleep or usleep to control the trigger frequency 4, crontab + php5, and web trigger modes. this is also different. one is timed trigger, and the other is periodic trigger, DZ and PHPWind are both scheduled triggers. I used a periodic trigger.

If you have a host, you must use crontab to execute the command line. PHP does not have to be written in the code. it would be better to use crontab. if you use a loop, you 'd better separate the loop from the specific handler, otherwise, the memory will continue to increase.

This function-ignore_user_abort was found in the php Manual. This function can help us implement scheduled tasks like cron in linux. let's take a look at how to implement it. First, read the explanation of this function in the php Manual. Descriptionint ignore_user_abort ([bool $ setting]) Sets whether a client disconnect shoshould cause a script to be aborted. that is to say, the following program will be executed no matter whether the client closes the browser.

Let's look at its parameters: ParameterssettingIf not set, the function will only return the current setting. this function accepts a parameter to determine whether to enable the ignore_user_abort function.

Return value: Return ValuesReturns the previous setting, as a boolean. here, the previous setting is returned and bool is worth it. after my test, this statement is incorrect. the returned result is of the int type.

How can we use the php function to implement scheduled tasks? This function is set_time_limit. you can use set_time_limit0 to set the program running time to no limit. the default php running time is 30 seconds, and set_time_limit (0) the program can be executed without limit.

Before executing the program, add ignore_user_abort (1) and set_time_limit (0). how can I write the final program?

Example:

  1. Ignore_user_abort (); // run script in background
  2. Set_time_limit (0); // run script forever
  3. $ Interval = 30; // do every 15 minutes...
  4. Do {
  5. $ Fp = fopen('text3.txt ', 'A ');
  6. Fwrite ($ fp, 'test ');
  7. Fclose ($ fp );
  8. Sleep ($ interval); // wait 15 minutes
  9. } While (true );
  10. ?>

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.