Idempotent systemd-Timer Maintenance Job Prompt
Write a safe, idempotent maintenance job (script plus systemd service and timer units) that can run on a schedule, survive overlaps and missed runs, and never corrupt state when run twice.
- Target user
- SREs and Linux admins scheduling recurring automation
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior SRE who builds scheduled jobs that are safe to run repeatedly, concurrently, or after a missed window. Correctness under re-runs matters more than cleverness. I will provide: - What the job does (e.g., prune old artifacts, sync a cache, rotate keys) - The schedule and tolerance for missed runs (must it catch up after downtime?) - The state it reads/writes and what "already done" looks like for this run Your job: 1. **Make the work idempotent** — design each step to check current state before acting (exists? already pruned? checksum matches?) so a second run is a no-op, not a duplicate. 2. **Prevent overlap** — guard the script with `flock` (or systemd `RefuseManualStart`/single-instance semantics) so a slow run can't collide with the next trigger. 3. **Write the units** — produce a `Type=oneshot` `.service` and a `.timer` with `OnCalendar=`, `Persistent=true` for catch-up, and a small `RandomizedDelaySec` to avoid thundering herds across hosts. 4. **Run least-privilege** — set `User=`, and sandbox with `ProtectSystem`, `ProtectHome`, `PrivateTmp`, and `ReadWritePaths` scoped to exactly what it touches. 5. **Add a dry-run + logging** — support `--dry-run`, log to the journal with clear start/skip/done lines, and exit non-zero on real failure so `systemctl` marks it failed. 6. **Verify** — show `systemd-analyze verify`, a manual `systemctl start`, two back-to-back runs proving the second is a no-op, and how `Persistent=true` triggers a catch-up after boot. Output: the script, both unit files, install/enable commands, and the verification transcript.
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
-
Bash Cron-to-systemd-Timer Migration Prompt
Convert legacy crontab entries into equivalent systemd service+timer units with correct scheduling, logging, environment, and failure handling so scheduled jobs become observable and manageable.
-
Idempotent Bulk File Rename & Migrate Tool Prompt
Write a Python tool that safely renames or relocates large batches of files using a rule set, with mandatory dry-run, collision detection, atomic moves, and an undo manifest so a bad run is reversible.
-
Bash Config File Diff and Safe Merge Prompt
Create a Bash script that compares a shipped default config against a live one, shows the drift, and merges new keys without overwriting local edits.
-
Systemd Timer Job Script Prompt
Convert a cron job into a robust systemd service + timer with proper logging, failure handling, sandboxing, and overlap prevention — or generate the units and accompanying script from scratch.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
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.