Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeCursor

Python Process Watchdog and Auto-Restart Prompt

Build a Python watchdog that monitors a long-running process or endpoint and restarts it with backoff when it hangs, crashes, or fails a health probe.

Target user
Platform engineers keeping unsupervised jobs and daemons alive
Difficulty
Intermediate
Tools
Claude, Cursor

The prompt

You are a senior reliability engineer who writes supervisor tooling that must never make an outage worse by flapping a service.

I will provide:
- What to watch (a PID, a command to (re)launch, or an HTTP health URL)
- The failure signals that count as "unhealthy"
- Restart limits and any maintenance windows

Your job:

1. **Define health** — implement a single `is_healthy()` that checks the process and/or probes the endpoint with a timeout; never block forever.
2. **Supervise** — loop on a fixed interval, but isolate each check in `try/except` so a transient error does not kill the watchdog itself.
3. **Back off** — on repeated failures, restart with exponential backoff plus jitter, and enforce a max-restarts-per-window circuit breaker that stops flapping.
4. **Restart safely** — terminate the old process with SIGTERM, wait, then SIGKILL only if needed; confirm the new PID is actually healthy before counting success.
5. **Add a dry-run** — `--dry-run` logs intended restarts without spawning anything.
6. **Log structured events** — emit JSON lines (timestamp, state, action, attempt) for ingestion.
7. **Shut down cleanly** — handle SIGINT/SIGTERM to stop supervising and optionally drain the child.

Output as: (a) the full script with type hints, (b) a config example, (c) a systemd unit to run the watchdog.

Cap restarts and trip the breaker; an infinite restart loop is worse than a clean failure.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Related prompts

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.