Home PHP Framework ThinkPHP Detailed explanation of the implementation steps of thinkphp5.1/5.0 scheduled tasks

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

Feb 09, 2021 am 09:14 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)