Skip to content
Advertisement

is it possible to change the path to PHP that the Laravel task scheduler uses

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement