Home > PHP Framework > ThinkPHP > body text

Detailed explanation of the implementation steps of thinkphp5.1/5.0 scheduled tasks

藏色散人
Release: 2021-02-09 09:14:48
forward
4804 people have browsed it

The tutorial column of thinkphp below will give you a detailed explanation of the steps to implement thinkphp5.1/5.0 scheduled tasks. I hope it will be helpful to friends in need!

What I mainly do is the function of sending text messages to employees on their birthdays. I run the script once a day.

The first step:

a.App Create a command folder under /module/

b. I created it in the admin module and created a SendMessage.php file under the command folder (the specific name is determined by yourself according to your needs)

c. Copy the following code to SendMessage.php

<?php
 namespace app\admin\command;
  
 use think\console\Command;
 use think\console\Input;
 use think\console\Output;
 use think\Db;
 use think\Log;
  
 class SendMessage extends Command
 {
     protected function configure(){
         $this->setName(&#39;SendMessage&#39;)->setDescription("计划任务 SendMessage");
     }
  
     //调用SendMessage 这个类时,会自动运行execute方法
     protected function execute(Input $input, Output $output){
         $output->writeln(&#39;Date Crontab job start...&#39;);
         /*** 这里写计划任务列表集 START ***/
  
         $this->birthday();//发短信
  
         /*** 这里写计划任务列表集 END ***/
         $output->writeln(&#39;Date Crontab job end...&#39;);
     }
  
     //获取当天生日的员工 发短信
     public function birthday()
     {
         echo &#39;这里写你要实现的逻辑代码&#39;;
     }
 }
Copy after login

Step 2: Add

return [&#39;app\admin\command\SendMessage&#39;];
Copy after login

in APP/command.php

Step 3: Set up crontab scheduled tasks

  1. ##crontab -l //Scheduled task list

  2. crontab -e //Edit to add

  3. crontab -r //Delete

In order to facilitate testing, you can first set it to execute once every minute and record the log /www/wwwroot/tool/runtime/message/2019.log

  1. */1 * * * * php /www/wwwroot/tool/think SendMessage>>/www/wwwroot/tool/runtime/message/2019.log 2>&1

  2. ##//Monitor whether your script is normal

  3. ##tail -f /www/wwwroot/tool/ runtime/message/2019.log

The above is the detailed content of Detailed explanation of the implementation steps of thinkphp5.1/5.0 scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template