Home >Backend Development >PHP Tutorial >PHP scheduled task implementation code example

PHP scheduled task implementation code example

WBOY
WBOYOriginal
2016-07-25 08:59:031072browse
  1. //Even if the Client is disconnected (such as closing the browser), the PHP script can continue to execute.
  2. ignore_user_abort();
  3. //The execution time is unlimited, the default execution time of PHP It is 30 seconds. Through set_time_limit(0), the program can be executed without limit
  4. set_time_limit(0);
  5. // Run every 5 minutes
  6. $interval=60*5;
  7. do{
  8. $url = “http:// /bbs.it-home.org";
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, $url);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($ch, CURLOPT_TIMEOUT, 2 );
  13. $result = curl_exec($ch);
  14. curl_close($ch);
  15. // Wait for 5 minutes
  16. sleep($interval);
  17. }while(true);
  18. ?>
Copy code

As long as you run the page above and then close it, the program will continue to run.

2. crontab The function of the crontab command is to schedule the execution of some commands at certain intervals. How to use crontab: crontab [ -e | -l | -r ] file name -e: edit task -l: display task information -r: delete scheduled execution task information The format of crontab:

  1. */5 * * * * /usr/bin/curl "http://bbs.it-home.org"
  2. #Visit bbs.it-home.org every 5 minutes
Copy Code

You may also like: The implementation principle of PHP scheduled tasks How to execute PHP program regularly with crontab under Linux



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