在aws弹性豆茎上运行cron作业 - django

时间:2022-08-25 09:36:19

I'm having trouble getting my cron jobs to execute.

我无法让我的cron作业执行。

Setup:

Django - 1.9

Django - 1.9

Elastic beanstalk - 64bit Amazon Linux 2016.03 v2.1.3 running Python 3.4

Elastic beanstalk - 运行Python 3.4的64位Amazon Linux 2016.03 v2.1.3

I've tried doing this a couple of ways so far:

到目前为止,我尝试过几种方法:

  1. Using a cron.yaml file: Didn't touch anything else - just added a cron.yaml file to my project root folder

    使用cron.yaml文件:没有碰到任何其他东西 - 只是将cron.yaml文件添加到我的项目根文件夹

    
    version: 1
    cron:
    - name: "test" url: "http://website.com/workers/test" schedule: "*/10 * * * *"
  2. Using a container command and a separate cron.txt file:

    使用容器命令和单独的cron.txt文件:

Added this line in my .ebextensions/development.config file

在我的.ebextensions / development.config文件中添加了这一行

05_some_cron:
    command: "cat .ebextensions/crontab.txt > /etc/cron.d/crontab && chmod 644 /etc/cron.d/crontab"
    leader_only: true

and in .ebextensions/crontab.txt

在.ebextensions / crontab.txt中

*/10 * * * * source /opt/python/run/venv/bin/activate && python mysite/manage.py test

The app deploys successfully in both cases.

该应用程序在两种情况下都能成功部署。

  1. Manually (in a browser) going to http://website.com/workers/test has the intended outcome (in the first case).
  2. 手动(在浏览器中)访问http://website.com/workers/test有预期的结果(在第一种情况下)。

  3. Adding source /opt/python/run/venv/bin/activate && python mysite/manage.py test as a management command runs the correct script once on deploying.
  4. 添加source / opt / python / run / venv / bin / activate && python mysite / manage.py test作为管理命令,在部署时运行一次正确的脚本。

The logs do not show any GETS on that url.

日志不会在该URL上显示任何GETS。

What am I doing wrong? Am I missing some step of the process or some setup step on EBS?

我究竟做错了什么?我是否错过了EBS流程或设置步骤的一些步骤?

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.

另外,为EBS托管的django应用程序运行cron作业的最佳方法是什么? - django应用程序可以在命令行中运行管理命令,如在尝试2中那样,或者通过扩展GET或POST URL来进行尝试1。

2 个解决方案

#1


2  

This works for me in Django 1.10.6 + AWS ElasticBeanstalk + Cronjob

这适用于Django 1.10.6 + AWS ElasticBeanstalk + Cronjob

* * * * * source /opt/python/run/venv/bin/activate && cd /opt/python/current/app/ && python manage.py do_your_thing > $HOME/cron-sqs.log 2>&1

* * * * * source / opt / python / run / venv / bin / activate && cd / opt / python / current / app / && python manage.py do_your_thing> $ HOME / cron-sqs.log 2>&1

do_your_thing is a management command

do_your_thing是一个管理命令

#2


0  

2018 Update using Django 2.0.6 + AWS ElasticBeanstalk + Cronjob

2018使用Django 2.0.6 + AWS ElasticBeanstalk + Cronjob进行更新

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".

我发现我需要使用source / opt / python / current / env导出AWS环境变量,以防止manage.py抛出错误“SECRET_KEY设置不能为空”。

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.

这是因为我把我的Django Secret密钥放在os.environ中用于beanstalk,默认情况下shell / cron似乎无法访问它。

See https://forums.aws.amazon.com/thread.jspa?threadID=108465

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).

我最后的cron作业.txt脚本如下。 (process_emails是我自己的django管理功能,myjob.log是记录函数调用输出的地方)。

*/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

#1


2  

This works for me in Django 1.10.6 + AWS ElasticBeanstalk + Cronjob

这适用于Django 1.10.6 + AWS ElasticBeanstalk + Cronjob

* * * * * source /opt/python/run/venv/bin/activate && cd /opt/python/current/app/ && python manage.py do_your_thing > $HOME/cron-sqs.log 2>&1

* * * * * source / opt / python / run / venv / bin / activate && cd / opt / python / current / app / && python manage.py do_your_thing> $ HOME / cron-sqs.log 2>&1

do_your_thing is a management command

do_your_thing是一个管理命令

#2


0  

2018 Update using Django 2.0.6 + AWS ElasticBeanstalk + Cronjob

2018使用Django 2.0.6 + AWS ElasticBeanstalk + Cronjob进行更新

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".

我发现我需要使用source / opt / python / current / env导出AWS环境变量,以防止manage.py抛出错误“SECRET_KEY设置不能为空”。

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.

这是因为我把我的Django Secret密钥放在os.environ中用于beanstalk,默认情况下shell / cron似乎无法访问它。

See https://forums.aws.amazon.com/thread.jspa?threadID=108465

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).

我最后的cron作业.txt脚本如下。 (process_emails是我自己的django管理功能,myjob.log是记录函数调用输出的地方)。

*/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