Search code examples
djangocronamazon-elastic-beanstalk

How to set a cronjob to run a Django management command in AWS elastic beanstalk


I have a Django application running in AWS Elastic beanstalk.I need to run a cron job which runs a Django management command every 10 minutes (python manage.py test). For that I created a .ebextensions/cron.config file.

.ebextensions/cron.config
container_commands:
  01_some_cron_job:
    command: "cat .ebextensions/cron_test.txt > /etc/cron.d/cron_test && chmod 644 /etc/cron.d/some_cron_job"

.ebextensions/cron_test.txt

*/10 * * * * /opt/python/run/venv/bin/python34 /opt/python/current/app/manage.py  test

Is this the right way to run a Django management command as cron job in AWS elastic beanstalk? Do we need to activate the virtual environment before running the command?


Solution

  • Yes, you need to run your command in the context of the virtual environment that Elastic Beanstalk has set up, but no, you do not need to activate said environment before running your command. At least that is my understanding of it. Example file: .ebextensions/cron.config

    files:
        "/etc/cron.d/mycron":
            mode: "000644"
            owner: root
            group: root
            content: |
                [email protected]
    
                */10 * * * * root source /opt/python/current/env && python3 /opt/python/current/app/manage.py test
    

    No guarantee that this will work - I am testing it out myself right now so there might be something wrong, but one of my coworkers got this general method to work on their site.

    The documentation about Elastic Beanstalk cronjobs, but not about the virtual environment, is located here: https://aws.amazon.com/premiumsupport/knowledge-center/cron-job-elastic-beanstalk/