Implementation example of php scheduled tasks

Source: Internet
Author: User
Recently, because the project needs to regularly perform some processing on the remote database, we need to do something like a scheduled task. after efforts, we finally implemented this scheduled task using php, the following describes the implementation process. this php implementation of the scheduled task mainly uses... recently, because the project needs to regularly perform some processing on the remote database, we need to do something like a scheduled task. after efforts, we finally implemented this scheduled task using php, the following describes the implementation process.

This php implementation mainly uses the ignore_user_abort () set_time_limit (0) sleep () functions.

The PHP instance code is as follows:

 

// The specific code is as follows:

 '; Sleep ($ interval); // wait for 5 s} while (true); // method 2 --- sleep regularly executes require_once '. /curlClass. php '; // introduce the file $ curl = new httpCurl (http: // pic4.phprm.com/2014/06/18/'www.jpg); // instantiate $ stime = $ curl-> getmicrotime (); for ($ I = 0; $ I <= 10; $ I ++) {echo 'test '. time ().'
'; Sleep ($ interval); // wait for 5 s} ob_flush (); flush (); $ etime = $ curl-> getmicrotime (); echo ''; echo round ($ etime-stime), 4); // the program execution time phprm.com?>

Function int ignore_user_abort:

The function name itself can be interpreted as "ignoring user impact", because the so-called user refers to the client, that is, the browser, so it is further interpreted as "ignoring browser impact ", so what is the impact? the impact refers to the closure of the browser and exceptions.

That is to say, the php program with this function will continue to execute even when the browser is turned off. for example, you need to execute a piece of code for 100 seconds, but this time is too long. Generally, you can't wait until 60 seconds.

If the program is terminated at this time, it may cause data exceptions, inconsistencies, or errors. you can use the program to continue running. its parameters are true and false, true is ignored, false is not ignored.

In the specific implementation process, I personally feel that PHP's scheduled task execution efficiency is not high. I suggest you hand over the scheduled task execution to shell. Relatively speaking, this method is too hard to implement, while shell is professional.

Linux script program

The crontab command is used here:

crontab   filecrontab [ -u user ] [ -u user ] { -l | -r | -e }

Note: crontab is used to allow users to execute programs at a fixed time or interval. crontab is used to write shell scripts and PHP to call shell. this is a feature of linux, it should not be the characteristics of PHP language.

Execute scripts using URLs in Crontab

If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.

The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. the Lynx text browser opens the URL in dialog mode by default. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output. the code is as follows:

00 * * * * lynx -dump http://www.phprm.com /script.php

The following example uses the CURL access URL to execute the PHP script every five minutes. The Curl displays the output in the standard output by default and uses the "curl-o" option, you can also dump the script output to a temporary file. the code is as follows:

*/5 * * * * /usr/bin/curl -o temp.txt http://www.phprm.com /script.php

The following example uses the wget url to execute the PHP script every 10 minutes. The-q option indicates quiet mode, and "-O temp.txt" indicates that the output will be sent to a temporary file. the code is as follows:

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.phprm.com /script.php


Article link:

Save this article for favorites!

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.