Php scheduled task implementation principle, php scheduled task Principle

Source: Internet
Author: User
Tags crontab example

Php scheduled task implementation principle, php scheduled task Principle

AccordingPhpThe manual briefly introduces some related knowledge:

1. Connection processing:

InPHPInternal, the system maintains the connection status. There are three possible states:

0-NORMAL (NORMAL)
1-ABORTED (exit unexpectedly)
2-TIMEOUT (TIMEOUT)

WhenPHPThe connection is valid when the script runs normally. When the remote client is disconnected, the ABORTED status mark will be opened. The interruption of remote client connection is usually caused by the user clicking the STOP button. When the connection time exceedsPHPThe TIMEOUT status is enabled.

You can determine whether the script needs to exit when the client terminates the connection. Sometimes it is much more convenient to run the script completely, even if the Script output is not accepted by a remote browser. By default, the script will exit when the remote client connection is interrupted. This process can bePhpIgnore_user_abort of. ini or"Php_ Value ignore_user_abort "and ignore_user_abort () functions. If notPHPIgnore the user's interruption, and the script will be interrupted unless the trigger function is disabled through register_shutdown_function. When a remote user clicks the STOP button and the script tries to output data again,PHPThe connection is interrupted and the trigger function is called.

The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be setPhp"Php_ Value max_execution_time "parameter or set_time_limit () function. When the counter times out, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call the connection_status () function to check whether timeout has caused the function to be called. If timeout causes function call to be disabled, the function returns 2.

Note that the ABORTED and TIMEOUT statuses are valid at the same time. This tellsPHPIt is possible to ignore the user's exit operation.PHPNote that the connection has been interrupted but the script is still running. If the running time limit is reached, the script will be exited, And the set function to close and trigger will also be executed. The connection_status () function returns 3.

2. Related functions:

Int ignore_user_abort ([bool setting])
This function sets whether a client disconnect shocould cause a script to be aborted. it will return the previous setting and can be called without an argument to not change the current setting and only return the current setting.

Int connection_aborted (void)
Returns TRUE if client disconnected.

Int connection_status (void)
Returns the connection status bitfield.

To regularly update a file, the program runs automatically. Two methods are found on the Internet: ignore_user_abort () and crontab.

The ignore_user_abort () function can be used with set_time_limit (0) and sleep ($ interval) to automatically run and update programs. The following is an instance.


Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled. set_time_limit (0); // the execution time is unlimited. The default php Execution time is 30 seconds. You can use set_time_limit (0) to allow unlimited execution of the program $ interval = 60*5; // run do {$ fp = fopen('test.txt ', 'A'); fwrite ($ fp, 'test'); fclose ($ fp) every five minutes ); sleep ($ interval); // wait for 5 minutes} while (true );

 



As long as you run the above page, and then turn it off, the program will continue to run.


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.try2.org
Visit www.try2.org every 5 minutes

0 8 *** lynx http://www.try2.org
Visit www.try2.org at every morning

0 10 6*1-5 lynx http://www.try2.org
Visit www.try2.org on the 6th of each month and from Monday to Friday at AM.

0 5 7 8 * lynx http://www.try2.org
Visit www.try2.org at, January 1, 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.

Original article: http://hi.baidu.com/andylu1988/item/9674d31406ed61008ebde4b6

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.