Laravel5命令行执行定时任务

  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  即可定时执行脚本

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 1:InputChannel提供函数创建底层的Pipe对象 2: 1)客户端需要新建窗口 2)new ViewRo...
    自由人是工程师阅读 5,403评论 0 18
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,609评论 9 467
  • .bat脚本基本命令语法 目录 批处理的常见命令(未列举的命令还比较多,请查阅帮助信息) 1、REM 和 :: 2...
    庆庆庆庆庆阅读 8,224评论 1 19
  • 文/安静de角落 我们对于炒饭的热情可能来自饭菜二合一的便捷与其N种口味的组合搭配,虽然不是五星推荐的早餐食谱,但...
    安静de角落LY阅读 385评论 1 9