Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 926
Dustfinger Avatar asked Jul 21 '16 23:07

Dustfinger


People also ask

How do I run a cron job in django?

Steps to setup a cron job in Django Create a file anywhere within your django's project according to your module directory structure for e.g. myapp/cron.py and define the function which you want to be executed automatically via cron. This will be your cron job. We define a cron just as above.

When should you not use Elastic Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.

Do Cronjobs run automatically?

The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.


1 Answers

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
like image 153
Ganondorfz Avatar answered Oct 11 '22 21:10

Ganondorfz