ThinkPHP5 定时任务

  1. 创建一个PHP 继承自Command
namespace app\index\controller;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Task extends Command
{
    protected function configure()
    {
        $this->setName('task')
            ->setDescription('定时计划:每天生成一个日期文件');
    }

    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 '这里写你要实现的逻辑代码';
    }

}
  1. 修改command.php
return ['app\index\controller\Task'];
  1. 设置crontab计划任务.定时任务的实现 主要是依赖于 操作系统
crontab -l //计划任务列表
crontab -e //编辑新增
crontab -r //删除
  1. crontab -e 新增一个
    1 0 * * * php /Library/WebServer/Documents/tp5/think task>>/Library/WebServer/Documents/tp5/runtime/2019.log 2>&1
    每天0点1分 执行
//监控一下你的脚本是不是正常的
tail -f /Library/WebServer/Documents/tp5/runtime/2019.log

你可能感兴趣的:(ThinkPHP5 定时任务)