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

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站