1.在console目录,用命令 php artisan make:command StatSchedule,代码如下:

<?php

namespace App\Console\Commands;
...

class StatSchedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'stat:log'; //定义schedule名称 

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $yesterday = date('Y-m-d',Time::yesterday()[0]);
        $yesterdayStart = date('Y-m-d H:i:s',Time::yesterday()[0]);
        $yesterdayEnd = date('Y-m-d H:i:s',Time::yesterday()[1]);
        //......业务逻辑
        }
    }
}

2.在Kernel.php中,编辑如下:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\StatSchedule::Class,  //类
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //
        $schedule->Command('stat:log')->dailyAt('17:13'); // 定义执行时间

    }
}

3.linux上crontab -e,编辑 

* * * * * /app.../bin/php /data-1/.../artisan schedule:run> /dev/null 

保存 

通过以上操作,即可完成操作。

 

 

 

 

 

更多推荐

laravel中schedule实现计划任务详解