Monitor Python Script Exit Codes in Cron

Python

The Problem

When Python scripts exit with a non-zero code, cron silently ignores the failure. You need a way to capture and alert on bad exit codes.

The Solution

Use a shell wrapper or Python's atexit module to send the exit code to CronRabbit. Non-zero codes trigger an immediate alert.

Why Exit Codes Matter

A cron job that exits with code 1 (or any non-zero value) has failed. But cron doesn't check — it just moves on. CronRabbit can differentiate between exit 0 (success) and any other code (failure).

Shell Wrapper Approach

The simplest method is wrapping your Python script in a shell one-liner that captures $? and appends it to the ping URL.

Native Python Approach

Use sys.exit() with explicit codes and send the status from within Python using a try/finally block.

Code Examples

Shell wrapper for exit code capture

Bash
# In your crontab:
0 3 * * * python3 /scripts/etl.py; curl -fsS -m 10 --retry 5 --retry-all-errors https://ping.cronrabbit.com/your-id/$?

Native Python exit code reporting

Python
import sys
import requests

PING = "https://ping.cronrabbit.com/your-id"

def main():
    try:
        run_etl_pipeline()
        requests.get(f"{PING}/0", timeout=10)
    except Exception:
        requests.get(f"{PING}/1", timeout=10)
        sys.exit(1)

if __name__ == "__main__":
    main()

Ready to Monitor Your Cron Jobs?

Create your free account and add your first monitor in under 5 minutes. No credit card required.