PHP implements scheduled task execution. php implements task _ PHP Tutorial

Source: Internet
Author: User
Tags sleep function
PHP implements scheduled task execution, and php implements the task. PHP implements scheduled task execution. This article describes how php implements scheduled task execution. the code is simple and practical. Share it with you for your reference. Specific implementation of PHP to implement scheduled task execution, php to implement the task

This example describes how to implement scheduled task execution in PHP. the code is simple and practical. Share it with you for your reference.

The specific implementation method is as follows:

Ignore_user_abort (true); // when the client is disconnected, ignore the script abort (allow the script to be executed all the time) set_time_limit (0); // set the maximum script execution time, 0 do {$ handle = fopen('auto.txt ', 'w'); if ($ fp) {$ text = 'Hello \ n \ r'; $ count = 0; for ($ I = 1; $ I <10; $ I ++) {if (! $ C = fwrite ($ handle, 'dide '. $ I. 'row :'. $ text) // returns the number of written characters. if a failure occurs, false {echo 'quarter 'is returned '. $ I. 'write failed! ';}$ Count + = $ c ;}} fclose ($ handle); sleep (60); // wait 60 seconds for execution} while (true );

I hope this article will help you with PHP programming.


How does PHP implement regular script execution?

If you want to use the php file, you can barely
There is a sleep function in php. the specific usage is sleep (time). the time here is measured in seconds.
First, create a php script
While (1 ){
// Yourcode
Sleep (3600*24 );
}
?>
Yourcode here is the function that you want to execute PHP scripts. although it can achieve this goal, there are also sacrifices. you need to access this script through URL at A.M, that is to say, the PHP file is executed, and the link cannot be interrupted. it must be persistent !! Otherwise, it will be invalid.

PHP is executed in a single request.

Q: How can I enable the system to execute a task on a regular basis in PHP?

Ignore_user_abort (); // run in the background
Set_time_limit (0); // keep running
$ Interval = 30; // Set the scheduled value
Do {
// Do what you want to do
Sleep ($ interval); // waiting for the waiter
} While (true );
?>

Examples in this article describes how PHP implements scheduled task execution. the code is simple and practical. Share it with you for your reference. Specific implementation...

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.