Yii2安装任务调度扩展

一、安装扩展

  在安装之前添加仓库到composer.json文件中

  "repositories": [{"type": "vcs", "url": "https://git.oschina.net/zjq528/yii2-cron.git"}],

 在项目的根目录下运行composer命令
 composer require webjson/yii2-cron

二、使用

<?php
namespace consolecontrollers;

use webjsoncronScheduleController;

class TestController extends ScheduleController {

  public function actionRun()
  {
    // 每小时执行一次 hello/index 命令
    $this->command('hello/index')->everyHour();
  }
}

ScheduleController是一个抽象类,里面有一个抽象方法 actionRun 这里就放要定时执行的任务

最后在crontab里面添加

* * * * * php yii test/run
原文地址:https://www.cnblogs.com/zhangjianqiang/p/7054919.html