简体   繁体   English

Laravel 5通过AWS EC2上的cron进行计划 - 命令未运行

[英]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. 我有一个Laravel 5.4工匠任务,我需要通过cron运行 - 但尽管命令和调度程序(显然)设置正确,但它仍未完成。

Is this a Laravel, php, apache, linux or crontab issue ? 这是一个Laravel,php,apache,linux还是crontab问题? 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: 在默认(亚马逊AMI)EC2实例上,artisan命令被正确定义并在通过以下方式调用项目目录( /var/www/html/myproject/ )时完美运行:

php artisan mycommand:option1

I've added this to a schedule into app/Console/Kernel.php which looks like this: 我已将此添加到app/Console/Kernel.php ,如下所示:

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

    $schedule   ->command('mycommand:option2')
                        ->dailyAt('07:15')
                        ->emailOutputTo('email@email.com');
}

Added the following cron command for apache via sudo crontab -u apache -e : 通过sudo crontab -u apache -eapache添加了以下cron命令:

* * * * * 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 ec2-user通过crontab -e
  • root via sudo crontab -e root via sudo crontab -e

System Output 系统输出

from sudo tail -f /var/log/cron : 来自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 运行Laravel 5.4.16由php artisan --version确定
  • running PHP 7.1.3 as determined by php -v 运行PHP 7.1.3由php -v确定

the issue was related to php missing its (absolute) path in the cron command definition 问题与php在cron命令定义中缺少(绝对)路径有关

the cron command should have been: cron命令应该是:

* * * * * /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 你可以从终端的which php输出中获取正确的php路径

Notes: 笔记:
- Laravel Scheduler commands work fine from apache user by adding cron commands via: - 通过以下方式添加cron命令,Laravel Scheduler命令可以从apache用户正常工作:

sudo crontab -u apache -e  

- Laravel still not logging the Log::info('schedule:run'); - Laravel仍未记录Log::info('schedule:run'); each minute like it should... even when running cron commands from root (ie setting cron via sudo crontab -e ) 就像它应该的每一分钟......即使从root运行cron命令(即通过sudo crontab -e设置cron)
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 这可能与Laravel中的其他一些设置有关 - 因为即使通过php artisan schedule:run手动运行Scheduler也不记录任何内容php artisan schedule:run从项目root php artisan schedule:run

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. 通常,经验法则是检查日志文件的写入权限,以确保apache用户可以写入。

If all else fails you can explicitly point to your log file in the crontab: 如果所有其他方法都失败了,您可以在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. 如果您的作业需要访问资源(如DB),那么您可能需要在调用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. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM