AWS上的Laravel计划任务无法正常工作 [英] Laravel Scheduling Task on AWS is not working
问题描述
我正在将我的Laravel应用程序部署到ElasticBeanstalk环境.我正在尝试在服务器上的调度任务"中运行命令.但这是行不通的.这就是我所做的.
I am deploying my Laravel application to ElasticBeanstalk environment. I am trying to run a command in the Scheduling Task on the server. But it is not working. This is what I have done.
我在Kernel.php文件中安排我的命令,如下所示.
I schedule my command in the Kernel.php file as follow.
$schedule->command('counter:update')->everyMinute()->onOneServer();
我正在使用Redis作为缓存驱动程序,并且可以正常工作.
I am using Redis as my cache driver and it is working.
然后我尝试了两种不同的方法.
Then I tried two different approaches.
第一种方法:使用Laravel AWS Worker软件包, https://packagist.org/packages/dusterio/laravel-aws-worker
我按照文档中的说明安装了软件包.
I installed the package following the instructions mentioned in the doc.
然后我将CSRF中间件中文档中提到的工作路由列入白名单.
Then I whitelist the worker routes mentioned in the doc in the CSRF middleware.
然后我在项目的根文件夹中创建了一个cron.yml文件,内容如下.
Then I created a cron.yml file right inside the project's root folder with the following content.
version: 1
cron:
- name: "schedule"
url: "/worker/schedule"
schedule: "* * * * *"
然后,我部署了我的应用程序.但是计划"任务无法正常工作.因此,我尝试了以下第二种方法.
Then I deployed my application. But the Scheduling task is not working. So I tried the second approach as follow.
第二种方法:使用Crontab
我创建了一个.ebextensions/cronjob.config文件,其中包含以下内容.
I created a .ebextensions/cronjob.config file with the following content.
files:
"/etc/cron.d/schedule_run":
mode: "000644"
owner: root
group: root
content: |
* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
然后,我部署了我的应用程序.第二种方法也不起作用.配置中缺少什么?如何使它正常工作?
Then I deployed my application. The second approach is not working either. What is missing in my configuration and how can I get it working?
这是我的另一个cronjob.config文件.
This is my another cronjob.config file.
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/myscript.sh
"/usr/local/bin/myscript.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
date > /tmp/date
* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
exit 0
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/mycron.bak"
推荐答案
您尝试的 cron.yml
仅用于工作环境.
/opt/elasticbeanstalk/support/envvars
仅适用于 Amazon Linux 1 .由于您正在使用运行PHP 7.3的 64位Amazon Linux 2 v3.0.3
(我记得),因此cron会出错.
The /opt/elasticbeanstalk/support/envvars
is only for Amazon Linux 1. Since you are using 64bit Amazon Linux 2 v3.0.3 running PHP 7.3
(as I recall), the cron will error out.
因此,您应该专注于第二种方法,该方法与 AWS博客相一致,说明如何在AL2上使用cron:
Thus, you should focus on the second approach, which is in line with AWS blog on how to use cron on AL2:
因此,我建议通过博客中显示的脚本(例如,/usr/local/bin/myscript.sh
)执行 cron
作业.
Therefore, I would recommend execute cron
job through a script (e.g., /usr/local/bin/myscript.sh
) as shown in the blog.
/usr/local/bin/myscript.sh
的修改内容.
您只能先尝试 date>/tmp/date
只是为了检查cron作业是否正常.为此,您必须部署应用程序,并检查/tmp/date
是否包含此脚本生成的时间.
You can try first only with date > /tmp/date
just to check if the cron job works. For this you have to deploy your application, and check if /tmp/date
contains the time generated by this script.
"/usr/local/bin/myscript.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
date > /tmp/date
# the line below is commented out.
#/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
exit 0
Amazon Linux 2中的访问环境变量
env变量存储在/opt/elasticbeanstalk/deployment/env
因此要在脚本中加载它们,可以使用以下内容:
Thus to load them in your script you can use the following:
env $(cat /opt/elasticbeanstalk/deployment/env | xargs) sh -c '/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1'
或两行
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
这篇关于AWS上的Laravel计划任务无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!