15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel]タスクスケジュール設定Cronインストールから

Last updated at Posted at 2016-11-16

cronインストール

動作確認

/etc/rc.d/init.d/crond status
  • インストールされているかを確認

インストール

yum install -y crontabs vixie-cron
  • インストール確認
/etc/rc.d/init.d/crond status

cron起動

/etc/rc.d/init.d/crond start

自動起動の確認

chkconfig --list crond

サービスを追加

chkconfig --add crond

crontabにLaravelのタスクエントリを追加

crontab -e
* * * * * php [プロジェクトのパス]/artisan schedule:run >> /dev/null 2>&1
  • 以上でcronが起動して毎分Laravelのタスクスケジュールが起動します
  • ログを確認する時は「/var/log/cron」から確認できます。

スケジュール追加

  • /app/Console/Kernel.phpを編集
    protected $commands = [
        \App\Console\Commands\Inspire::class,
        \App\Console\Commands\Hoge::class,  // 追加
    ];


    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')->hourly();
        $schedule->command('hoge')-> everyFiveMinutes();  // 5分毎に実行
    }
  • /app/Console/Commands/hoge.phpを作成
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;



class Hoge extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'hoge';

    /**
     * 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()
    {
        Storage::disk('local')->put(date("Ymd_His").'.txt', 'message');
    }
}
  • これで5分毎にログを記録するスケジュールが作成されます
15
14
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
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?