Fulfillment of php scheduled tasks

Source: Internet
Author: User
Php job scheduling implementation (zt) article classification: PHP Programming & lt ;? Phpignore_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, and the default php execution time is 30 seconds. through set_time_limit (0), the program can implement unlimited php scheduled tasks.

Implementation of php scheduled tasks (zt)
Article Category: PHP programming
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 limit the execution of the program.
$ Interval = 60*5; // run every 5 minutes
Do {
$ Fp = fopen('test.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait for 5 minutes
} While (true );
?>


Php scheduled task introduction: I was particularly interested in se before A.M. on, but I didn't know java, lucene and other search engine development tools, so I kept exploring the functions of php.

Finally, we found that php can also be captured, and the principle is very easy: directly obtain the page source file, and then obtain the required information through the reference truncation of the regular expression or string. However, the performance cannot be compared with the multi-thread crawling of search engines.

After implementing the previous step, I thought again that if the capture can be automatically obtained at a scheduled time, the manual execution of the executable page would be saved.

Later, I learned about the effect of "scheduled tasks" in some open-source php programs: you can regularly run a program, such as database backup, update cache, generate static pages, and generate website maps.

Recently, because the project needs to regularly update the remote database to a local location, I found it online.

Ignore_user_abort (); functions can be used with set_time_limit (0); and sleep ($ interval); to achieve the above automatic update.

First, a basic paradigm is given, with a personal test program:

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

First run the program, then close the page, and then fill in the text3.txt file every 30 seconds when the program is still running.

Implementation effect (omitted)

Finally, some related knowledge is briefly introduced according to the php manual:

1. connection processing:

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

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

When the PHP script runs normally, the connection is valid. 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 exceeds the PHP time limit, the TIMEOUT status mark will be opened.

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 be controlled by php. ini's ignore_user_abort or by the "php_value ignore_user_abort" and ignore_user_abort () functions in Apache. conf settings. If PHP is not told to ignore user interruptions, 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, PHP will detect that the connection has been interrupted and call to disable the trigger function.

The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be changed by setting the "php_value max_execution_time" parameter in the max_execution_time or Apache. conf setting of php. ini or the 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. It is possible to tell PHP to ignore the user's exit operation. PHP will still note that the user has interrupted the connection 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.


This article from The CSDN blog, reproduced please indicate the source: http://blog.csdn.net/webdesman/archive/2009/09/02/4510372.aspx
?

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.