douda5227 2017-08-30 13:22
浏览 74
已采纳

是否可以更改Laravel任务调度程序使用的PHP路径

I have a Laravel 5.4 app which is on shared hosting and the cron job isn't working. I have set the command up in the kernel.php like so:

$schedule->command('eoddsmaker:get_events')
         ->withoutOverlapping()
         ->appendOutputTo(storage_path('logs').'/cron-get_events.log')
         ->everyMinute();

And if I just run /usr/bin/php-5.6 artisan eoddsmaker:get_events from the command line it runs fine. When it gets called by the cron job though it doesn't run. This is my cron definition:

* * * * * /usr/bin/php-5.6 /var/sites/c/cyo.mydomain.com/artisan schedule:run >> /var/sites/c/cyo.mydomain.com/cron.log 2>&1

I can see from the cron logs on the server that this task is running every minute and everytime that it runs the following output gets added to the cron.log file:

Running scheduled command: '/usr/bin/php' 'artisan' eoddsmaker:get_events > '/dev/null' 2>&1
X-Powered-By: PHP/5.6.8
Content-type: text/html; charset=UTF-8

So to dig a bit deeper if I look in the cron-get_events.log file that I have configured the task to send output to the following gets output every time it runs:

Warning: Unexpected character in input:  '\' (ASCII=92) state=1 in /var/sites/c/cyo.mydomain.com/artisan on line 31

Parse error: syntax error, unexpected T_STRING in /var/sites/c/cyo.mydomain.com/artisan on line 31

Because I'm on shared hosting the default PHP version is 5.2 and I have to add a rule in the htaccess file to get it to use PHP 5.6. If I forget to add the rule I get the same error as the one that is in the cron-get_events.log so this leads me to believe that the reason the command isn't working is because when the scheduler runs it is calling the command with /usr/bin/php as the path to PHP rather than /usr/bin/php-5.6

Is there a way to configure the task scheduler to use a different path to PHP?

  • 写回答

1条回答 默认 最新

  • doubi4435 2017-08-30 13:39
    关注

    I've managed to solve the problem by changing my scheduler task definition from:

    $schedule->command('eoddsmaker:get_events')
         ->withoutOverlapping()
         ->appendOutputTo(storage_path('logs').'/cron-get_events.log')
         ->everyMinute();
    

    To:

    $schedule->exec('/usr/bin/php-5.6 /var/sites/c/cyo.mydomain.com/artisan eoddsmaker:get_events >> /var/sites/c/cyo.mydomain.com/cron.log 2>&1')
         ->withoutOverlapping()
         ->appendOutputTo(storage_path('logs').'/cron-get_events.log')
         ->everyMinute();
    

    I'd still be interested to know through if there is a nicer way to achieve this so that I can just declare the path to PHP within Laravel

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器