Skip to main content

Get An Email If Any Django Management Command Fails

After a bit of Googling, I found this post which details several methods of getting this done. I only needed an email sent to admins if any management command fails; below is the code I came up with.

Edit manage.py to look something like this (I’ve only included code needed for this functionality):

# ...
import sys
import traceback

from django.core.mail import mail_admins


def main():

    # ...

    try:
        execute_from_command_line(sys.argv)
    except Exception:
        mail_admins(
            subject=f"Command <{' '.join(sys.argv)}> failed!",
            message=traceback.format_exc(),
            fail_silently=False,
        )

# ...

With that, if an exception is raised during the running of any command, an email is sent to email addresses specified in settings.ADMINS with the full traceback.