Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 schedule via cron on AWS EC2 - commands not running

Problem:
I have a Laravel 5.4 artisan task that I need to run via cron - but it is not being completed despite the Command and Scheduler being (apparently) set-up correctly.

Is this a Laravel, php, apache, linux or crontab issue ? What's the best way to diagnose ?


Background
On default (amazon AMI) EC2 instance, the artisan command is defined correctly and runs perfectly from the project directory (which is /var/www/html/myproject/) when called via:

php artisan mycommand:option1

I've added this to a schedule into app/Console/Kernel.php which looks like this:

protected function schedule(Schedule $schedule)
{
    Log::info('schedule:run');
    $schedule   ->command('mycommand:option1')
                        ->dailyAt('07:00')
                        ->emailOutputTo('[email protected]');

    $schedule   ->command('mycommand:option2')
                        ->dailyAt('07:15')
                        ->emailOutputTo('[email protected]');
}

Added the following cron command for apache via sudo crontab -u apache -e:

* * * * * php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1

To ensure it's not a permissions issue I also added the same command for the following users :

  • ec2-user via crontab -e
  • root via sudo crontab -e

System Output

from sudo tail -f /var/log/cron :

Apr 11 19:17:01 ip-10-0-0-42 CROND[17968]: (root) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:17:01 ip-10-0-0-42 CROND[17969]: (ec2-user) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:17:01 ip-10-0-0-42 CROND[17970]: (apache) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:18:01 ip-10-0-0-42 CROND[17980]: (ec2-user) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:18:01 ip-10-0-0-42 CROND[17981]: (apache) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:18:01 ip-10-0-0-42 CROND[17982]: (root) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:19:01 ip-10-0-0-42 CROND[17992]: (root) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:19:01 ip-10-0-0-42 CROND[17993]: (ec2-user) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)
Apr 11 19:19:01 ip-10-0-0-42 CROND[17994]: (apache) CMD (php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1)

nothing appearing in either of these:
sudo tail -f /var/www/html/myproject/storage/log/laravel.log
or
sudo tail -f /var/www/html/myproject/storage/log/laravel-2017-04-11.log


Additional Info

Kernel permissions:

drwxr-sr-x 2 apache apache 4096 Feb 24 00:24 Commands
-rw-r--r-- 1 apache apache 1111 Feb 24 00:24 Kernel.php

Resources checked:

  • https://askubuntu.com/q/23009
  • https://github.com/laravel/framework/issues/13462
  • https://stackoverflow.com/a/30664353/3092596
  • https://laracasts.com/discuss/channels/servers/how-to-perform-php-artisan-schedulerun-on-an-ubuntu-server-with-laravel-53-for-cron-jobs?page=1

Other info:

  • running Laravel 5.4.16 as determined by php artisan --version
  • running PHP 7.1.3 as determined by php -v
like image 350
goredwards Avatar asked Apr 11 '17 22:04

goredwards


2 Answers

the issue was related to php missing its (absolute) path in the cron command definition

the cron command should have been:

* * * * * /usr/local/bin/php /var/www/html/myproject/artisan schedule:run >> /dev/null 2>&1

you can get the correct php path from the output of which php in terminal

Notes:
- Laravel Scheduler commands work fine from apache user by adding cron commands via:

sudo crontab -u apache -e  

- Laravel still not logging the Log::info('schedule:run'); each minute like it should... even when running cron commands from root (ie setting cron via sudo crontab -e)
This is probably related to some other setting in Laravel - as it doesn't log anything even when Scheduler is run manually via php artisan schedule:run from project root

like image 100
goredwards Avatar answered Nov 19 '22 02:11

goredwards


Typically, a rule of thumb would be to check write permissions on your log file to ensure it is write-able by the apache user.

If all else fails you can explicitly point to your log file in the crontab:

* * * * * /usr/local/bin/php /var/www/html/myproject/artisan schedule:run >> /var/www/html/myproject/storage/logs/laravel.log 2>&1

If your jobs needs access to resources such as the DB, then you may want to source an environment variables definition file before calling artisan. Something like so:

* * * * * source /path/to/envvars; /usr/local/bin/php /var/www/html/myproject/artisan schedule:run >> /var/www/html/myproject/storage/logs/laravel.log 2>&1

Good luck.

like image 1
user4603841 Avatar answered Nov 19 '22 02:11

user4603841