PHP scheduled tasks

Source: Internet
Author: User
Tags php web development crontab example
We know that PHP in Linux can easily implement task scheduling, while the implementation of PHP task scheduling on Windows is complicated, the implementation of PHP scheduled tasks is very useful in php web development. for example, tasks such as scheduled Mysql database backup, cache update, data collection, and timed generation of static pages can be implemented. The PHP ignore_user_abort function can continue to execute tasks when the client is interrupted (such as disabling the browser. The following is an example code that uses the PHP ignore_user_abort function to implement PHP scheduled tasks.

PHP scheduled task instance code

Copy content from PHP Code to clipboard

PHP scheduled task example: in the above code, the main function is to automatically write a piece of text into the file through the PHP file write operation every 15 seconds. when the user closes the browser, the program continues to run.

Row 2nd: first run the php ignore_user_abort function to inform the system to ignore the user's abandonment and continue execution. by default, the PHP script exits when the remote client connection is interrupted. Use the php ignore_user_abort function to control whether to ignore user interruption and continue executing PHP code.

Knowledge Point: ignore_user_abort (setting) setting is optional. If it is set to true, the disconnection from the user is ignored. if it is set to false, the script stops running. If this parameter is not set, the current setting is returned.

Row 3rd: does not limit the running time of PHP scripts. We know that the default PHP script has a limited time, which is generally set through the max_execution_time option in the PHP. INI configuration file. You can use the PHP set_time_limit function to change the specific time. if it is set to 0, the time is not limited.

Row 4th: set the specific interval, in seconds.

Line 5-21: The phpfile write operation is performed continuously at intervals.

Note: after executing this PHP code in the browser, the background will always execute this code at a specified interval unless the WEB server is interrupted or the system is shut down.

So far, the tutorial on how to implement PHP scheduled tasks through the PHP ignore_user_abort function is complete. by understanding the implementation principles of this instance, you will know how to implement the regular database backup, regular generation of static pages, and other functions in many PHP blogs.

Using ignore_user_abort () to implement php files can run after the browser is closed.

Of course, to keep the program running, you must add set_time_limit (0 );

In Linux, there is a simpler method, that is, the crontab command.

The crontab command schedules the execution of some commands at certain intervals.

Crontab usage: crontab [-e |-l |-r] file name-e: edit task-l: Display task information-r: delete scheduled execution task information

Crontab format:

* *** Command

Command to be run by hour, day, month, and week

Crontab example:

*/5 * lynx http://www.php100.com

Access www.php100.com every 5 minutes

0 8 *** lynx http://www.php100.com

Visit www.php100.com at every morning.

0 10 6*1-5 lynx http://www.php100.com

Visit www.php100.com on the 6th of each month and from Monday to Friday at AM.

0 5 7 8 * lynx http://www.php100.com

Visit www.php100.com at on January 26, August 7.

The above special meanings:

"*" Indicates all numbers in the value range, "/" indicates each meaning, and "*/5" indicates every five units, "-" represents a number from a number to a number, "," separate several discrete numbers.

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.