Report this

What is the reason for this report?

How to regularly run Django management commands on App Platform

Posted on August 16, 2022

I’d like to try using App Platform to run some Django sites. Almost every site I’ve made requires one or more commands to be run periodically (e.g. once an hour, once a day, etc). On a VPS I’d use cron to do this, and on Heroku I’d use Heroku Scheduler (with less flexibility).

But I can’t see a way to do this on App Platform. I assume there’s something obvious I’ve missed, as it seems quite a basic feature?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello, at this time App Platform doesn’t have support to run scheduled tasks (aside from pre/post deployment jobs).

However, if you’re willing to run Django in a container image that your own, you can run cron in tandem with Django to execute whatever management commands that you’d like.

For example:

FROM python:buster

# Install cron (also anything else for Django)
RUN apt-get update \
    && apt-get install -y cron \
    && apt-get update -y 

# Assuming you have some scripts that might execute some
# management commands
COPY *.sh /scripts/
RUN chmod +x /scripts/*.sh

WORKDIR /scripts

# Run at the beginning of the hour
RUN echo "0 * * * * root /scripts/management_command.sh" >/etc/cron.d/management_command

# Run both Django and cron together
CMD [ "sh", "-c", "cron && <cmd to run Django>" ]

Hope this helps!

Greg

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.