在AWS Elastic Beanstalk上运行Cron作业-Django [英] Running cron jobs on aws elastic beanstalk - django
问题描述
我无法执行我的cron作业。
I'm having trouble getting my cron jobs to execute.
设置:
Django-1.9
Django - 1.9
Elastic beanstalk-运行Python 3.4的64位Amazon Linux 2016.03 v2.1.3
Elastic beanstalk - 64bit Amazon Linux 2016.03 v2.1.3 running Python 3.4
我已经尝试过这样做到目前为止,有几种方法:
I've tried doing this a couple of ways so far:
-
使用cron.yaml文件:
没有碰其他任何东西-只是在我的项目根文件夹中添加了一个cron.yaml文件
Using a cron.yaml file: Didn't touch anything else - just added a cron.yaml file to my project root folder
version: 1
cron:
- name: "test"
url: "http://website.com/workers/test"
schedule: "*/10 * * * *"
使用容器命令和单独的cron.txt文件:
Using a container command and a separate cron.txt file:
在我的.ebextensions / development.config文件中添加了这一行
Added this line in my .ebextensions/development.config file
05_some_cron:
command: "cat .ebextensions/crontab.txt > /etc/cron.d/crontab && chmod 644 /etc/cron.d/crontab"
leader_only: true
和.ebextensions / crontab.txt
and in .ebextensions/crontab.txt
*/10 * * * * source /opt/python/run/venv/bin/activate && python mysite/manage.py test
在两种情况下,应用均成功部署。
The app deploys successfully in both cases.
- 手动(在浏览器中)转到 http:/ /website.com/workers/test 具有
的预期结果(在第一种情况下)。 - 添加
source / opt / python / run / venv / bin / activate&& python mysite / manage.py test
作为管理命令,在部署时会运行正确的脚本。
- Manually (in a browser) going to http://website.com/workers/test has the intended outcome (in the first case).
- Adding
source /opt/python/run/venv/bin/activate && python mysite/manage.py test
as a management command runs the correct script once on deploying.
日志没有在该URL上显示任何GETS。
The logs do not show any GETS on that url.
我在做什么错?我是否错过了过程中的某些步骤或EBS上的某些设置步骤?
What am I doing wrong? Am I missing some step of the process or some setup step on EBS?
还有什么最佳方法可以为EBS上托管的Django应用程序运行cron作业? -django应用程序可以像尝试2一样从命令行运行管理命令,也可以像尝试1那样通过扩展GET或POST URL来运行管理命令。
Also what is the best ways to run cron jobs for django applications hosted on EBS? - django apps can run management commands either from the command line as in attempt 2 or by extending a GET or POST url as in attempt 1.
推荐答案
2018更新,使用 Django 2.0.6 + AWS ElasticBeanstalk + Cronjob
2018 Update using Django 2.0.6 + AWS ElasticBeanstalk + Cronjob
我发现我需要使用源/ opt / python / current / env导出AWS环境变量,以防止manage.py引发错误 SECRET_KEY设置不能为空。
I found I needed to export the AWS environment variables using source /opt/python/current/env to prevent manage.py from throwing the error "The SECRET_KEY setting must not be empty".
这是因为我已将Django秘密密钥放置在os.environ中用于beantalk,默认情况下,shell / cron无法访问它。
This is because I had placed my Django Secret key in the os.environ for beanstalk which it seems is not accessible by shell/cron by default.
请参见 https: //forums.aws.amazon.com/thread.jspa?threadID=108465
我最后的cron作业.txt脚本如下。 (process_emails是我自己的 django管理功能,myjob.log是记录函数调用输出的位置)。
My final cron job .txt script was as follows. (process_emails is my own django management function, myjob.log is where the output of the function call is logged).
*/15 * * * * source /opt/python/run/venv/bin/activate && cd /opt/python/current/app/ && source /opt/python/current/env && python manage.py process_emails >> /var/log/myjob.log 2>&1
这篇关于在AWS Elastic Beanstalk上运行Cron作业-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!