Dustfinger
Dustfinger

Reputation: 1023

Running cron jobs on aws elastic beanstalk - django

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

Setup:

Django - 1.9

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:

  1. 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 * * * *"
  2. Using a container command and a separate cron.txt file:

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

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.

  1. Manually (in a browser) going to http://website.com/workers/test has the intended outcome (in the first case).
  2. Adding source /opt/python/run/venv/bin/activate && python mysite/manage.py test as a management command runs the correct script once on deploying.

The logs do not show any GETS on that url.

What am I doing wrong? Am I missing some step of the process or some setup step on 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.

Upvotes: 6

Views: 4024

Answers (2)

Ganondorfz
Ganondorfz

Reputation: 331

2018 Update using 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".

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.

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

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

Upvotes: 5

Karl Zillner
Karl Zillner

Reputation: 614

This works for me in 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

do_your_thing is a management command

Upvotes: 3

Related Questions