PHP scheduled task implementation and Linuxcrontab scheduled task

Source: Internet
Author: User
Tags crontab example
Sometimes scheduled tasks are required to monitor a program. The program must run automatically. 1. the ignore_user_abort () function can be used with set_time_limit (0) and sleep ($ interval) to automatically update programs. Example: 1234567891011121314151617 even if Cl... sometimes requires scheduled tasks to monitor a program. The program must run automatically.
1. ignore_user_abort ()
The ignore_user_abort () function works with set_time_limit (0) and sleep ($ interval) to automatically update programs.
Example:

// Even if the Client is disconnected (such as closing the browser), the PHP script can continue to be executed.

Ignore_user_abort ();

// The execution time is unlimited. the default php execution time is 30 seconds. you can use set_time_limit (0) to allow unlimited execution of the program.

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 for 5 minutes

Sleep ($ interval );

} While (true );

As long as you run the above page, and then turn it off, the program will continue to run.
2. crontab
In Linux, there is a simpler method, that is, the crontab command.
The crontab command schedules the execution of some commands at certain intervals.
Crontab usage: crontab [-e |-l |-r] file name-e: edit task-l: Display task information-r: delete scheduled execution task information
Crontab format:
* *** Command
Command to be run by hour, day, month, and week
Crontab example:

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.