作成したバッチをタスクスケジュールで管理すれば、1つ1つCronにバッチを設定する手間がなくなります。
1つ1つCronにバッチを設定する場合
cron
0 7 1 * * cd /var/my-project; php artisan sendsalesfile 0 1 * * * cd /var/my-project; php artisan deletelog 30 * * * * cd /var/my-project; php artisan updateschedule
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\SendSalesFileCommand::class, \App\Console\Commands\DeleteLogCommand::class, \App\Console\Commands\UpdateScheduleCommand::class, ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('inspire') // ->hourly(); } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path('routes/console.php'); } }
タスクスケジュールをCronで使用する場合
cron
* * * * * cd /var/my-project; php artisan schedule:run
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\SendSalesFileCommand::class, \App\Console\Commands\DeleteLogCommand::class, \App\Console\Commands\UpdateScheduleCommand::class, ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('sendsalesfile')->cron('0 7 1 * *'); // Cron指定に基づきタスク実行 $schedule->command('deletelog')->dailyAt('1:00'); // 毎日1:00に実行 $schedule->command('updateschedule')->everyThirtyMinutes(); // 30分毎にタスク実行 } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path('routes/console.php'); } }