1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravel 5.7 で cron

Last updated at Posted at 2019-03-11

laravel cron やろうぜ

cron の内容を更新する場合は以下を参考
https://qiita.com/ma7ma7pipipi/items/d65caa48a96aace39df9

Console/Commands/Cron.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Log;

class Cron extends Command
{
    protected $signature = 'cron';
    protected $description = 'Cron description';

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

    public function handle()
    {
        Log::debug(time()." ハンドルcron ログ動作");
    }
}


Console/Kernel.php

<?php

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

class Kernel extends ConsoleKernel
{

    protected $commands = [
    ];


    protected function schedule(Schedule $schedule)
    {
        $schedule->command('cron')
            ->evenInMaintenanceMode()//メンテナンスモードでも動かす
            ->everyMinute(); //毎分動かす
//            ->cron('5 */1 * * *'); //1時間毎に 5 分になったら動かす

//        複数個 cron を仕掛けたい場合はふくすうファイルを作るだけ。
        //        $schedule->command('cronanalytics')
//            ->evenInMaintenanceMode()//メンテナンスモードでも動かす
//            ->cron('3 */1 * * *'); //1時間に1度動かす
    }

    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
        require base_path('routes/console.php');
    }
}


分 1-59
時 0-23
日 1-31
月 1-12
曜日 0-6 ( 日曜日が0となります。 )
コマンド スクリプトなど、実行できるもの。
->cron('* * * * *');//分 時 日 月 曜日 コマンド

cronをしかける

cron.txt

* * * * * /opt/remi/php71/root/usr/bin/php -c /etc/opt/remi/php71/php.ini /var/www/html/mama-rich.net/artisan schedule:run 1>> /dev/null 2>&1

再読み込み

crontab cron.txt

これでログができる。

以下古い記事

php artisan make:command  GetItemFeed --command="getitem"

以下のファイルができる。
そして処理したい内容は handle() 内に書く。

ログを取得するプログラムを書く。

/app/Console/Commands/GetItemFeed.php
use Log;

    public function handle()
    {
        Log::debug(time()." cron ログ動作");
    }


実行しようぜ
ログファイルにログが記述される。


php artisan getitem
// [2019-03-11 13:55:01] production.DEBUG: 1552280101 cron ログ動作  

Cron登録準備

kernel.php

    protected $commands = [
        \App\Console\Commands\GetItemFeed::class, // ←今回の追加
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('getitem')
            ->evenInMaintenanceMode()//メンテナンスモードでも動かす
            ->everyMinute(); //毎分動かす
    }

それではCronに登録
実行日時は laravelで判断するので、すべて * にしておく。


* * * * * /opt/remi/php71/root/usr/bin/php -c /etc/opt/remi/php71/php.ini /var/www/html/helpee.club/artisan schedule:run 1>> /dev/null 2>&1


ログができてるかチェック


[2019-03-11 13:56:01] production.DEBUG: 1552280161 cron ログ動作  
[2019-03-11 13:57:01] production.DEBUG: 1552280221 cron ログ動作  
[2019-03-11 13:58:02] production.DEBUG: 1552280282 cron ログ動作  


やったね!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?