Home>Article>PHP Framework> thinkphp method to set up scheduled execution tasks
1. Method 1: v3.2.1
①.ThinkPHP/Library/Behavior/CronRunBehavior.class.php file
First of all, What we are talking about is this automatic execution task file. There is a bug in this file provided by the official. I am using version v3.2.1. You can try it if there are corrections in later versions.
checkTime(); } } private function checkTime() { if (F('CRON_CONFIG')) { $crons = F('CRON_CONFIG'); } else if (C('CRON_CONFIG')) { $crons = C('CRON_CONFIG'); } if (!empty($crons) && is_array($crons)) { $update = false; $log = array(); foreach ($crons as $key => $cron) { if (empty($cron[2]) || $_SERVER['REQUEST_TIME'] > $cron[2]) { G('cronStart'); R($cron[0]); G('cronEnd'); $_useTime = G('cronStart', 'cronEnd', 6); $cron[2] = $_SERVER['REQUEST_TIME'] + $cron[1]; $crons[$key] = $cron; $log[] = 'Cron:' . $key . ' Runat ' . date('Y-m-d H:i:s') . ' Use ' . $_useTime . ' s ' . "\r\n"; $update = true; } } if ($update) { \Think\Log::write(implode('', $log)); F('CRON_CONFIG', $crons); } } } }
②、tgs.php
Create a new tags.php file in the Application/Common/Conf folder to set the tags.
'配置值' 'app_begin' =>array('Behavior\CronRunBehavior'), );
③、config.php
Configure the config.php file in the Application/Common/Conf folder for automatic operation.
true, // 是否开启自动运行 'CRON_CONFIG' => array( '测试执行定时任务' => array('Home/Index/crons', '5', ''), //路径(格式同R)、间隔秒(0为一直运行)、指定一个开始时间 ), );
④、IndexController.class.php
Write scheduled execution tasks in the Application/Home/Controller/IndexController.class.php file.
show('','utf-8'); } */ public function index() { $contents = file_get_contents("test.txt"); //每次访问此路径将内容输出,查看内容的差别 var_dump($contents); exit; $this->assign("contents", $contents); $this->display(); } //定时执行的方法 public function crons() { //在文件中写入内容 file_put_contents("test.txt", date("Y-m-d H:i:s") . "执行定时任务!" . "\r\n:)
欢迎使用 ThinkPHP!
", FILE_APPEND); } }
In this way, we have written the scheduled execution task. Every 5 seconds, we access the URL of any project, and then check the test.txt file in the root directory to find the changes in the content.
Note: When you modify the interval, you will find that it does not take effect. This is because you need to delete the cache file in the Runtime/Data folder. The interval cache is stored in the CRON_CONFIG.php file.
2. Method 2: v3.2.2
This method is not much different from method one.
①、tags.php
Create a new tags.php file in the /Application/Common/Conf directory. (This is the same as method 1)
'配置值' 'app_begin' =>array('Behavior\CronRunBehavior'), );
②, crons.php
Create a new crons.php file in the /Application/Common/Conf directory. (This is different from method 1, please pay attention to the distinction.)
array('myplan', 2, nextruntime), );
③, myplan.php
Create a new Cron folder in the /Application/Common/ directory, and create a new file myplan.php in it document.
";
At this point we can access the url of the project, and then we will find that the ~crons.php file is generated in the Application/Runtime/ directory. The content of the file is as follows:
array ( 0 => 'myplan', 1 => 60, 2 => 1398160322, ), ); ?>
Recommended tutorial:thinkphp tutorial
The above is the detailed content of thinkphp method to set up scheduled execution tasks. For more information, please follow other related articles on the PHP Chinese website!