PHP implementation of scheduled tasks

Source: Internet
Author: User
: This article describes how to implement scheduled tasks in PHP. For more information about PHP tutorials, see. PHP is relatively weak in this regard. if you only use php, you can implement the following:

<? Php ignore_user_abort (); // after closing the browser, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 1; // How long do {$ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com \ n "); fclose ($ fp); sleep ($ sleep_time);} while (true);?>

But when I execute the script, even if I close the browser, I cannot stop this program at all, so you need a script execution switch, you can use the method introduced by external files to implement the include switch variable during the while loop. Then we can achieve this:
Create an external variable file switch. php with the following content:

<? Phpreturn 1; // 1 execution script 0 exit execution script?>

The script for improvement is as follows:

<? Php ignore_user_abort (); // after the browser is closed, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 5; // How long does it take to execute $ switch = include 'switch. php '; while ($ switch) {$ switch = include' switch. php'; $ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com $ switch \ n"); fclose ($ fp ); sleep ($ sleep_time);} exit ();?>

This script is only feasible for testing and should be less efficient. for lamp, you can use crontab.
Add a small piece of code:

Ignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program $ interval = 60*30; // run do every half an hour {// Here is the code you want to execute sleep ($ interval); // wait 5 minutes} while (true );

So many articles about the implementation of PHP scheduled task execution will be introduced to you later. do not miss out.

The above describes how to implement scheduled tasks in PHP, including some content, and hope to help those who are interested in PHP tutorials.

Related Article

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.