Home  >  Article  >  Backend Development  >  Detailed explanation of PHP implementation of scheduled tasks, _PHP tutorial

Detailed explanation of PHP implementation of scheduled tasks, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:32807browse

Detailed explanation of how PHP implements scheduled tasks.

PHP should be said to be relatively weak in this regard. If you only use PHP to implement it, it can be implemented as follows:

<&#63;php
 ignore_user_abort();//关闭浏览器后,继续执行php代码
 set_time_limit(0);//程序执行时间无限制
 $sleep_time = 1;//多长时间执行一次
 do{
 $fp = fopen('test.txt','a+');
 fwrite($fp,"这是一个php博客:phpddt.com \n");
 fclose($fp);
 sleep($sleep_time);
 }while(true);
 
&#63;>

But when I execute the script, even if I close the browser, I can’t stop the program at all, so you need a switch to execute the script, which you can do by introducing an external file. During the while loop, just include the switch variable. Then it can be achieved like this:
Create an external imported variable file switch.php with the following content:

<&#63;php
return 1;//1执行脚本 0退出执行脚本
&#63;>

The improved script is as follows:

<&#63;php
 ignore_user_abort();//关闭浏览器后,继续执行php代码
 set_time_limit(0);//程序执行时间无限制
 $sleep_time = 5;//多长时间执行一次
 $switch = include 'switch.php';
 while($switch){
 $switch = include 'switch.php';
 $fp = fopen('test.txt','a+');
 fwrite($fp,"这是一个php博客:phpddt.com $switch \n");
 fclose($fp);
 sleep($sleep_time);
 }
 exit();
 
&#63;>

This script is only feasible for testing, and the specific efficiency should not be high. For lamp, you can use crontab to implement it.
Add a little code:

ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行.
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去
$interval=60*30;// 每隔半小时运行
do{
  //这里是你要执行的代码  
  sleep($interval);// 等待5分钟
}while(true);

This is all about the implementation of PHP scheduled execution tasks. There will be related articles to share with you later, so don’t miss it.

Articles you may be interested in:

  • PHP gets the timestamp of a certain period of time php scheduled tasks
  • Linux uses crontab to implement PHP execution planned scheduled tasks
  • PHP version of cron scheduled task executor usage example
  • Using the sleep function in PHP to implement scheduled tasks example sharing
  • Implementation of delayed execution of PHP scheduled tasks
  • Debugging in WordPress Related PHP script examples for scheduled tasks

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084587.htmlTechArticleDetailed explanation of PHP implementation of scheduled tasks. PHP should be said to be relatively weak in this regard. If you only use PHP to implement it, you can As follows: php ignore_user_abort();//After closing the browser, continue executing the php code...
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