Laravel5.5でcronを使う方法。タスクスケジュールがすごい便利!

フレームワークってすごいな〜と思う今日この頃。
初めてLaravelを使ってサービスを作っているのですがごく一般的な仕組みとかであれば本当に簡単にできるんだなと関心しています。
で、今回、またすごいなこれと思ったのがLaravelのタスクスケージュール!
サーバー側で毎回、タスクごとにcronの設定をしていたのですがこれがけっこう面倒くさかった。
しかし、LaravelだといちいちサーバーにsshすることもなくLaravel側で設定できてしまうというスグレモノ!
まずは、サーバー側で

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

これだけcron登録、あとはLaravelでガンガン設定できます。

まずは、実行したいタスクを作ります。

php artisan make:controller TaskController

app/Http/Controllers/TaskController.php

class TaskController extends Controller
{

    // 毎日17時にメール送信
    public function send_mail()
    {
        // メール送信するプログラム

    }
}

続いてコマンド作成

$ php artisan make:command SendMailCommand

app/Console/Commands/SendMailCommand.php

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\TaskController;

class SendMailCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:send_mail'; //コマンド名

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'send mail 17:00'; //コマンド説明

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        TaskController::send_mail(); //実行するタスク
    }
}

カーネルに追記

app/Console/Kernel.php

<?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 = [
        //追記
        \App\Console\Commands\SendMailCommand::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();

        // withoutOverlappingをつけることで多重実行を防ぐので必須
        $schedule
        ->command('command:send_mail')
        ->withoutOverlapping()
        ->dailyAt('17:00');

    }

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

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

スケジュールの細かい設定は
https://readouble.com/laravel/5.5/ja/scheduling.html
以上で毎日17時にメールが送信されるようになります。