Monitor Django Management Commands with CronRabbit
PythonThe Problem
Django management commands invoked by cron (e.g. clearsessions, custom data imports) run without supervision. A database error or ORM exception can silently break the pipeline.
The Solution
Add CronRabbit pings directly inside your management command's handle() method. Track duration and report failures explicitly.
Management Commands in Cron
Running python manage.py your_command from cron is standard Django practice. But without monitoring, a failed migration, missing environment variable, or database timeout goes unnoticed.
Adding Monitoring
Import requests in your command and add pings to the handle() method. Send /start at the beginning and the success ping at the end.
Code Examples
Monitored Django management command
Pythonimport requests
from django.core.management.base import BaseCommand
PING = "https://ping.cronrabbit.com/your-id"
class Command(BaseCommand):
help = "Nightly data cleanup"
def handle(self, *args, **options):
requests.get(f"{PING}/start", timeout=5)
try:
self.cleanup_expired_sessions()
self.archive_old_records()
requests.get(PING, timeout=5)
self.stdout.write(self.style.SUCCESS("Done"))
except Exception as e:
requests.get(f"{PING}/fail", timeout=5)
raise