Skip to main content

Python cron jobs with Sentry's @monitor, locally

Set the Sentry DSN in your Python job to http://sentry@127.0.0.1:8000/1 and keep the @monitor decorator from sentry_sdk.crons. Each run then shows up in Buggregator under Sentry → Crons: started, finished, failed. Give the monitor a schedule and Buggregator also marks runs that never started and runs that never finished.

Steps

1. Start Buggregator with a database file

The default database lives in memory, so jobs and their runs are gone after a restart. Keep them in a file.

bash
docker run --pull always \
  -p 127.0.0.1:8000:8000 \
  -e DATABASE_DSN=/data/buggregator.db \
  -v buggregator-data:/data \
  ghcr.io/buggregator/server:latest

2. Point the SDK at Buggregator

Any key before @ works. The number at the end puts the data into the default project.

python
import sentry_sdk

sentry_sdk.init(dsn="http://sentry@127.0.0.1:8000/1")

In Docker Compose, use the service name: http://sentry@buggregator:8000/1.

3. Keep your decorator, add a schedule

The schedule lets Buggregator tell when a run is due. checkin_margin and max_runtime are minutes.

python
from sentry_sdk.crons import monitor

@monitor(monitor_slug='nightly-report', monitor_config={
    'schedule': {'type': 'crontab', 'value': '0 3 * * *'},
    'timezone': 'Europe/Berlin',
    'checkin_margin': 5,
    'max_runtime': 30,
})
def nightly_report():
    ...

The first run creates the job in Buggregator. The settings the SDK sends update it on every run. monitor_config needs sentry-sdk 1.45.0 or newer.

4. Open Sentry, then Crons

Each job shows its status, schedule, last and next run, and a strip of the last 20 runs. Jobs that failed, hung or missed a run come first, under Needs attention. Open a job for its run history with start time and duration.

Why it works

The decorator sends a check-in when the function starts and another when it ends: ok, or error if it raised. Buggregator stores them as runs. Every 30 seconds it checks the schedule: a run that was due and never came is missed, and a run that started but did not end within max_runtime is timeout. Nobody is notified; you see it on the page.

Checked on with Buggregator v2.13.0

Setup in 60 seconds

No registration. No account. Start it and point your app at it.

1

Start Buggregator

One file, installed with one command. The web UI opens on port 8000. Prefer containers? Use the Docker tab.

$ curl -fsSL https://raw.githubusercontent.com/buggregator/server/master/scripts/install.sh | sh
$ HTTP_ADDR=127.0.0.1:8000 \
  SMTP_ADDR=127.0.0.1:1025 \
  VAR_DUMPER_ADDR=127.0.0.1:9912 \
  MONOLOG_ADDR=127.0.0.1:9913 \
  PROXY_ADDR=127.0.0.1:8080 \
  ~/.local/bin/buggregator

The script picks the file for your CPU: Intel/AMD or ARM.

One file for Linux, macOS and Windows, about 40 MB. Nothing else to install.

This run line keeps it on this computer. Without the *_ADDR settings it listens on all network interfaces

Rather not run a script? Download the file yourself

Runs on Windows, macOS and Linux, on Intel/AMD and ARM.

Prefer not to do it yourself? Your AI agent can set Buggregator up for you