您的位置 首页 php

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

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

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

第一步:

a.App/模块/ 下创建command文件夹

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

c.复制下面的代码到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'];

第三步:设置crontab计划任务

  1. crontab -l //计划任务列表

  2. crontab -e //编辑新增

  3. crontab -r //删除

为了方便测试,可以先设置成每分钟执行一次 ,记录一下日志/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. //监控一下你的脚本是不是正常的

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

文章来源:智云一二三科技

文章标题:详解thinkphp5.1/5.0定时任务的实现步骤

文章地址:https://www.zhihuclub.com/25502.shtml

关于作者: 智云科技

热门文章

网站地图