PHP timed task implementation and Linux crontab timed tasks

Source: Internet
Author: User
Sometimes it takes a timed task to monitor a program. Requires the program to run automatically.
1, Ignore_user_abort ()
The Ignore_user_abort () function is paired with Set_time_limit (0) and sleep ($interval) to enable the program to run updates automatically.
Example:

PHP scripts can continue to execute even if the client disconnects (such as turning off the browser).

Ignore_user_abort ();

Execution time is unlimited, php default execution time is 30 seconds, through Set_time_limit (0) can allow the program to execute indefinitely

Set_time_limit (0);

Run every 5 minutes

$interval =60*5;

do{

$url = "Http://yaolei.info";

$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, $url);

curl_setopt ($ch, Curlopt_returntransfer, 1);

curl_setopt ($ch, Curlopt_timeout, 2);

$result = curl_exec ($ch);

Curl_close ($ch);

Wait 5 minutes

Sleep ($interval);

}while (TRUE);

Just run the page above and then turn it off, and the program will run all the way down.
2, Crontab
There's an easier way to do Linux under the crontab command.
The function of the crontab command is to schedule execution of some commands at certain intervals.
Crontab How to use: crontab [-e |-l |-r] FileName-E: Edit Task-L: Show task Information-r: Delete scheduled execution task information
Format of crontab:
* * * * * Command
Time-sharing day and month command to run
Examples of crontab:

  • 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.