Laravel5命令行执行定时任务

2018-02-27  本文已影响0人  misaka去年夏天
  1. 配置console的Kernel
protected $commands = [
        Commands\DoSomething::class,
    ];

protected function schedule(Schedule $schedule)
    {
        $schedule->command('aaa:bbbbb')-> everyMinute(); //每分钟执行一次
    }

  1. 新建command脚本
    新建目录App\Console\Commands
    在Commands目录下新建DoSomething类
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

/**
 * 处理JIRA的project数据
 */
class DoSomething extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'aaa:bbbbb'; //执行脚本的命令别名

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test schedule';

    function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        // 脚本的业务逻辑在这里写即可
    }

}

3. 执行命令
a. 在命令行执行php artisan schedule:run 将在1分钟后执行脚本
b. 在命令行执行php artisan aaa:bbbbb将立刻执行脚本
3. 在crontab里添加定时任务执行php artisan schedule:run  即可定时执行脚本

上一篇 下一篇

猜你喜欢

热点阅读