【Laravel5,Cron】エックスサーバー(xserver)でcommandが実行されない。
Laravelでcronを動かしてバッチ処理をしたかったので、実装したがcommandが実行されない……。
App\Console\Kernel.php
class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ \App\Console\Commands\GetOrderFromEmail::class, ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('getorder')->everyMinute(); } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path('routes/console.php'); } }
App\Console\Commands\GetOrderFromEmail
class GetOrderFromEmail extends Command { /** * レスポンスメッセージ */ protected $RES_NONTERGET = '対象のメールアカウントがありませんでした。'; protected $RES_ERROR_CONNECTION = 'メールアカウントへの接続でエラーが発生しました。'; /** * The name and signature of the console command. * * @var string */ protected $signature = 'getorder {--type=default} {--ago=1}'; /** * The console command description. * * @var string */ protected $description = '登録されているメールアカウントからメールを取得します。'; /** * チェック対象のメールリスト */ protected $mailLists; /** * メール受信設定 */ protected $AdminMailMonitoring; /** * コマンド実行時刻 */ protected $now; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); Log::info('GetOrderBatch Run Construct'); $nowHour = date('G'); /* $nowMin = date('i'); if($nowMin >= '00' && $nowMin < '30'){ $nowMin = '00'; } else{ $nowMin = '30'; } */ $this->now = $nowHour.'.00'; //.$nowMin; $this->AdminMailSetting = new AdminMailSetting; } /** * Execute the console command. * * @return mixed */ public function handle() { Log::info('GetOrderBatch Run '); try{ ...
これをcronで実行すると、コンストラクタのログだけが出力されて、 handle()を実行してくれない……。 Cronの設定は xserverを使用しているので、
/usr/bin/php7.0 /path/order/artisan schedule:run >>/path/order/cronlog.log 2>>/path/order/cronerrorlog.log
これでCron自体は動作していたけど、Cron側でメッセージがでていた。
No scheduled commands are ready to run.
このコマンドは動かせないよ!ってことかな? 海外フォーラムを調べてると /storage/framework 内を削除するといいよ! とのことなので削除。
Running scheduled command: '/usr/bin/php' 'artisan' getorder > '/dev/null' 2>&1 &
お動いてるっぽい。キャッシュだったのかな?
※/storage/framework内のフォルダを削除してしまうとシステムエラーが出るので、ファイルのみ削除すること。
しかしやはりコンストラクタのログしか出力されない……。 更に調べる。 execが動かないようになってるよとか色々原因は解決されてる記事はあるものの、 どれも状況が一致せずに悩む……。
頭抱えながらCronログをよく見てみる。
Running scheduled command: '/usr/bin/php' 'artisan' getorder > '/dev/null' 2>&1 &
(あれそういえばXserverのCron指定はphpのバージョンも記述必要だったよな??まさか) と思って $schedule->command()の中身を見てみると単純に exec()を実行しているっぽい。 なので、 $schedule->command()をやめて exec()を直接記述することにしてみる。
Kernel.php
//$schedule->command('getorder')->everyMinute(); $schedule->exec('/usr/bin/php7.0 artisan getorder')->everyMinute();
これで待つこと1分! う……動いた-!!! バッチリ処理内のログが出力されています。 php7.0で動かしてるシステムなんだから当然といえば当然というか。 xserverはphpの複数バージョンが動いてるので、いちいち指定が必要なんですね。 ちなみに /usr/bin/php だとphp5.3で動くっぽい。
ということで今回は、exec()で記述しちゃうか、command()で使うphp指定を書き換えとくか、 しとけば動きますね。 xserverのバージョン指定はコントロールパネルから確認可能です。 あーぁ疲れたぁ。












