详解thinkphp5.1/5.0定时任务的实现步骤

 3664

给大家详解thinkphp5.1/5.0定时任务的实现步骤,希望对需要的朋友有所帮助!

我主要做的是一个员工生日当天发短信的功能,每天跑一次脚本,

第一步:

1、App/模块/ 下创建command文件夹

2、我这边是创建在admin模块里面,在command文件夹下创建一个SendMessage.php文件(具体名字自己根据需求定)

3、复制下面的代码到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('SendMessage')->setDescription("计划任务 SendMessage");
    }
       
    //调用SendMessage 这个类时,会自动运行execute方法
    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/ 
        $this->birthday();//发短信
        
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }
       
    //获取当天生日的员工 发短信
    public function birthday()
    {
        echo '这里写你要实现的逻辑代码';
    }
}

第二步:在APP/command.php里面加上

return ['app\admin\command\SendMessage'];


602237494a039.jpg

第三步:设置crontab计划任务

crontab -l //计划任务列表
crontab -e //编辑新增
crontab -r //删除

为了方便测试,可以先设置成每分钟执行一次 ,记录一下日志/www/wwwroot/tool/runtime/message/2019.log

*/1 * * * * php /www/wwwroot/tool/think SendMessage>>/www/wwwroot/tool/runtime/message/2019.log 2>&1
//监控一下你的脚本是不是正常的
tail -f /www/wwwroot/tool/runtime/message/2019.log




本文网址:https://www.zztuku.com/detail-8645.html
站长图库 - 详解thinkphp5.1/5.0定时任务的实现步骤
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐