0
votes

I have written few django management commands that I want to run hourly, daily and weekly basis. I'm using Elastic Beanstalk and created a worker instance where the code is deployed. Can someone help me how to run the django management command with crontab using elastic beanstalk. Thanks

Here is my management command:

python manage.py command_name

Please help me write the container_command in .ebextensions/django.config file for crontab that will schedule the command on hourly basis. Thanks

Any help?

1
What have you tried so far?Shubhitgarg
container_commands: run_cron: command: */60 * * * * python /path/to/manage.py command_name Ammar
This is written in .ebextensions/django.config file.Ammar

1 Answers

0
votes

in .ebextensions create a file crontab.txt

# Set the cron to run with utf8 encoding
PYTHONIOENCODING=utf8

0 1 * * * root source /opt/python/current/env && nice /opt/python/current/app/manage.py command_name
# this file needs a blank space as the last line otherwise it will fail

This is a crontab file that will run command_name at 1 am every day. You'll need to get you path right which depends on your file structure.

In one of your config files include:

  02_cron_job:
    command: "cp .ebextensions/crontab.txt /etc/cron.d/my_cron_jobs && chmod 644 /etc/cron.d/my_cron_jobs"
    leader_only: true

This is going to copy your cron file into the right place on the ec2 instance so the job runs.