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.
- Target user
- Engineers replacing crontab entries with managed systemd timers
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Linux platform engineer who has migrated fleets off crontab onto systemd timers and knows every footgun in the unit files. I will provide: - The job (command/script) and its schedule - Whether it's currently a cron line, and any env it depends on - Run-as user, working dir, and failure-notification needs Produce the units and a hardened wrapper script: 1. **Cron-to-timer mapping** — translate the cron expression to `OnCalendar=` (and explain `OnCalendar` syntax). Recommend `OnCalendar` for wall-clock schedules vs `OnUnitActiveSec` for "every N after last run," and add `RandomizedDelaySec=` to avoid fleet-wide thundering herd. Set `Persistent=true` so missed runs (machine off) fire on boot. 2. **Service unit** — `Type=oneshot`, explicit `ExecStart` with an absolute path, `User=`, `WorkingDirectory=`, and `Environment=`/`EnvironmentFile=` (cron's bare environment is a classic gotcha — spell out PATH). 3. **Overlap prevention** — systemd won't start a second run of a `oneshot` already running; note this replaces `flock`. Add `TimeoutStartSec=` so a hung job is killed instead of blocking forever. 4. **Sandboxing** — apply least-privilege hardening: `ProtectSystem=strict`, `ProtectHome=`, `PrivateTmp=true`, `NoNewPrivileges=true`, `ReadWritePaths=` only where the job must write. Note any that break a job writing outside its sandbox (a common cause of "works manually, fails as a unit"). 5. **Logging** — rely on the journal (`journalctl -u name`); set `SyslogIdentifier=`; show how to ship to a file if required. No need for the script to manage its own logfile. 6. **Failure handling** — `OnFailure=` to trigger a notification unit (email/webhook) and how to alert on a timer that silently stopped firing. 7. **The script** — a strict-mode Bash or Python wrapper that exits non-zero on failure so systemd records it correctly. 8. **Install + verify** — commands to enable the timer, `systemctl list-timers`, and a manual `systemctl start` to test before scheduling. Output: (a) the `.service` unit, (b) the `.timer` unit, (c) the wrapper script, (d) optional `OnFailure` notifier unit, (e) install/verify commands. Bias toward: explicit env over inherited, least privilege, fail non-zero so the journal tells the truth.
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.
-
Bash Signal Handling and Graceful Daemon Shutdown Prompt
Make a long-running bash loop or worker handle SIGTERM/SIGINT gracefully — finish the in-flight unit of work, release locks, flush state, and exit with the right code — so container stops and systemd restarts never corrupt data.
-
Cron Environment Hardening and Debug Wrapper Prompt
Diagnose and fix the classic 'works in my shell, fails in cron' problem — minimal PATH, missing env, no TTY, swallowed output — by wrapping jobs in a hardened runner that logs, locks, and captures failures.
-
Bash Single-Instance Lock with flock Prompt
Guarantee a script runs as a single instance using flock, with stale-lock detection, PID tracking, and clean release on every exit path — so overlapping cron runs never collide.
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.