Laravel 5 Kernel 添加定时任务操作

在前一章内容,我们已经做了定时任务的功能。我们现在需要把定时任务操作条件到 Kernel。

添加方法:将操作名称添加到 command()里面,然后后边跟上定时任务规则。

 protected function schedule(Schedule $schedule)
    {
        $schedule->command('merchant-order:sync')->everyMinute();
    }

完整的代码如下:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('merchant-order:sync')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

然后在服务器上添加下面定时任务代码

* * * * * cd /home/laravel && php artisan schedule:run >> /dev/null 2>&1