Crontab in Linux to implement PHP planning tasks

Source: Internet
Author: User
Tags curl php script sleep centos

PHP programs sometimes need to execute regularly, using Linux crontab to execute PHP scripts, complete the PHP planning task. For example, the use of a lot of micro-blog app application-Del Piero time Machine, is to achieve the function of sending content in a timely way. Of course PHP also has the Ignore_user_abort function.
Neither of these methods is reliable or stable. We can use Linux's crontab tools to reliably trigger PHP to perform tasks.

Here are two ways to crontab.

First, use PHP to execute scripts in crontab
Just like calling a normal shell script in crontab, use a PHP program to invoke the PHP script.
The following myscript.php are performed every hour:


# CRONTAB-E
* * * * * */usr/local/bin/php/home/john/myscript.php/usr/local/bin/php is the path to the PHP program.

Ii. using URLs to execute scripts in crontab
If your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.
The following example uses a Lynx text browser to access URLs to execute PHP scripts every hour. The Lynx text browser opens the URL by default using the dialog method. However, like the following, we use the-dump option in the Lynx command line to convert the URL output to standard output.


* * * * * * * * * * * Lynx-dump http://www.centos.bz/myscript.php The following example is to use the Curl Access URL to execute PHP scripts every 5 minutes. Curl defaults to display output in standard output. With the "curl-o" option, you can also dump the output of the script to a temporary file.


*/5 * * * */usr/bin/curl-o temp.txt http://www.centos.bz/ Myscript.php of course, the way to complete the PHP task is more than win the following also have a planning task, the method has a lot of, but relatively stable, but also relatively good method is basically the use of Linux under this kind of crontab.

Do it directly in PHP

If executed by the page, set
<?php
Ignore_user_abort (TRUE);
Set_time_limit (0);
?>

Using Sleep (300) + dead loop,
So you can achieve the scheduled task, this program does not have output, there is easy to interrupt the output

The code is as follows Copy Code

<?php
Ignore_user_abort (TRUE);
Set_time_limit (0);

while (1) {
$fp = fopen (' Time_task.txt ', "A +");
$STR = Date ("y-m-d h:i:s"). " n ";
Fwrite ($fp, $STR);//log to Time_task.txt text
Fclose ($FP);
Sleep (1800); Run once in half an hour
}
?>

function int Ignore_user_abort:

From the function name itself, it can be interpreted as, "Ignore user impact"
Because the so-called user refers to the client, that is, the browser
So further explained as, "Ignore browser's influence"

So what does the impact refer to is the browser shutdown and the exception

In other words, there is this function in the PHP program, even when the browser is turned off, the program does not finish it will continue to execute until the execution

For example, you have a piece of code to perform 100 seconds, but this time is too long, the average user can not wait for 60 seconds when they can't stand it off
If the program is terminated at this time, it is likely to cause data anomalies, inconsistencies or errors, and you need the program to continue running

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.