goredwards
goredwards

Reputation: 2494

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 :


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:


Other info:

Upvotes: 6

Views: 5367

Answers (2)

user4603841
user4603841

Reputation: 1425

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.

Upvotes: 1

goredwards
goredwards

Reputation: 2494

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

Upvotes: 7

Related Questions