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

2

6

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:


Other info:

  • running Laravel 5.4.16 as determined by php artisan --version
  • running PHP 7.1.3 as determined by php -v
Weintrob answered 11/4, 2017 at 22:26 Comment(2)
I don't think that should be the correct answer, I'm facing the same problem, I tried the following options without any success - not including the '../artisan' at the end (is not a directory) - running the command on the root location. - Manually write the task on the crontab with 'crontab -e' - Restarting Apache - php artisan schedule:run I think that could be any external config on the EC2 instance that makes this go wrong. Hope this can help and open a new line of solutionsNatashianatassia
@DavidELaresS I'm sorry you've still got an issue with your EC2 system - however if a solution doesn't work for you doesn't mean it's not the correct answer to the stated problem. There are thousands of other variables in your system which might be still causing your issue. Please take the time to read the Laravel docs. Also, perhaps you could put any comments regarding the answer on the answer itself (rather than the question)? You could also ask a new question and mention that you tried this solution but it didn't work for you.Weintrob
W
7

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

Weintrob answered 11/4, 2017 at 22:56 Comment(2)
New laravel logs appear to be created by the 'webapp' user, and the user running cron does not have write access to the logs.Twocycle
Adding the ec2-user to the webapp group is a possible solution: usermod -a -G webapp ec2-userTwocycle
U
1

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.

Underskirt answered 22/8, 2018 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.