Php/laravel/laravel5/バッチ
ナビゲーションに移動
検索に移動
バッチ作成
$ php artisan make:console Robots --command="robots"
作られたバッチファイル
$ vi app/Console/Commands/Robots.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Robots extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'robots';
/**
* 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()
{
echo "robots";
}
}
バッチ本体に追加
Commands\Robots::class,を追加
$ vi app/Console/Kernel.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 = [
// Commands\Inspire::class,
+ Commands\Robots::class,
];
実行
$ php artisan robots robots
バッチのスケジュールを追加したい場合
$ crontab -e * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
$ vi app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('robots')
->cron('*/5 * * * * *');
}
参考:https://readouble.com/laravel/5/1/ja/scheduling.html
スケジュールの種類
->cron('* * * * * *'); CRON記法によるスケジュール
->everyMinute(); 毎分タスク実行
->everyFiveMinutes(); 5分毎にタスク実行
->everyTenMinutes(); 10分毎にタスク実行
->everyThirtyMinutes(); 30分毎にタスク実行
->hourly(); 毎時タスク実行
->daily(); 毎日深夜12時に実行
->dailyAt('13:00'); 毎日13:00に実行
->twiceDaily(1, 13); 毎日1:00と13:00時に実行
->weekly(); 毎週実行
->monthly(); 毎月実行
->yearly(); 毎年実行
アウトプットした場合
出力
$schedule->command('robots')
->cron('*/5 * * * * *')
->sendOutputTo('/tmp/robots.log');
追加出力
$schedule->command('robots')
->cron('*/5 * * * * *')
->appendOutputTo('/tmp/robots.log');
2重起動防止
$schedule->command('hogehoge')->withoutOverlapping();
withoutOverLapping()の部分
ロックファイルは以下にある
ls storage/framework/schedule-*