Home  >  Article  >  Backend Development  >  Principle of PHP5 scheduled task offline function_PHP tutorial

Principle of PHP5 scheduled task offline function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:42:431025browse

In order to update a certain file regularly, the program needs to run automatically. I found two methods from the Internet: ignore_user_abort() and crontab
The ignore_user_abort() function can be used with set_time_limit(0) and sleep($interval) to realize automatic program running and updating. Here is an example
ignore_user_abort(); //Even if the Client is disconnected (such as closing the browser), the PHP script can continue to execute.
set_time_limit(0); // The execution time is unlimited. The default execution time of PHP is 30 seconds. Through set_time_limit(0), the program can be executed without limit
$interval=60*5; // Run every 5 minutes
do{
$fp = fopen(test.txt,a);
fwrite($fp,test);
fclose($fp);
sleep($interval); // Wait 5 minutes
}while(true);
?>
As long as you run the page above and then close it, the program will continue to run.
There is a simpler method under Linux, which is the crontab command
The function of the crontab command is to schedule the execution of some commands at a certain time interval.
How to use crontab: crontab [ -e | -l | -r ] file name -e: edit task -l: display task information -r: delete scheduled execution task information
crontab format:
* * * * * Command
Minute Hour Day Month Weekday Command to run
Example of crontab:
*/5 * * * * lynx http://www.BkJia.com
Visit www.2cto.com every 5 minutes
0 8 * * * lynx http://www.BkJia.com
Visit www.2cto.com at 8am every day
0 10 6 * 1-5 lynx http://www.BkJia.com
Visit www.2cto.com on the 6th of every month and every Monday to Friday at 10am
0 5 7 8 * lynx http://www.BkJia.com
Visit www.2cto.com at 5am on August 7
Special meanings of the above:
"*" represents all numbers within the value range, "/" represents every, "*/5" represents every 5 units, "-" represents from a certain number to a certain number,", "Separate several discrete numbers.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486034.htmlTechArticleIn order to update a certain file regularly, the program needs to run automatically. I found two methods from the Internet: ignore_user_abort() and crontab ignore_user_abort() function with set_time_limit(0) and sleep...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn