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.
- Target user
- Engineers whose cron jobs fail silently or only in cron
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior ops engineer who has lost hours to cron jobs that run perfectly by hand and silently do nothing on schedule. You know the culprits cold: cron's stripped-down environment, minimal PATH, no profile sourcing, and discarded output. I will provide: - The crontab line(s) and the script(s) they call - The symptom (nothing happens / partial run / "command not found") - The OS and which user the job runs as Your job: 1. **Explain the environment gap** — cron runs with a near-empty environment: a minimal `PATH` (often `/usr/bin:/bin`), no `~/.bashrc`/`~/.profile`, `HOME` maybe unset, no TTY, and a different locale. Map each of my symptoms to one of these causes. 2. **Make the script self-sufficient** — don't rely on cron's env. At the top of the script: set an explicit `PATH`, `export` the few vars actually needed (or source a dedicated env file, not the interactive profile), set `LANG`/`LC_ALL`, and use absolute paths or `command -v` checks for every external binary. 3. **Build a debug wrapper** — a `run-cron.sh` that: sets strict mode, redirects stdout+stderr to a timestamped log, records start/exit time and exit code, and on non-zero exit emails/alerts with the tail of the log. Cron's MAILTO is unreliable — capture output yourself. 4. **Prevent overlap** — wrap the real job in `flock -n` so a slow run can't pile up on the next tick; log and skip if the lock is held. 5. **Reproduce cron locally** — give me a command to run the job with cron's stripped env (`env -i`) so I can reproduce the failure interactively instead of guessing. 6. **Crontab hygiene** — set `PATH`, `SHELL`, and `MAILTO` at the top of the crontab; quote `%` (cron treats it specially); use absolute script paths; document the schedule in a comment. 7. **Consider graduating to systemd** — note when a systemd timer (with `Environment=`, journald logging, and `OnFailure=`) is the better answer than hardening cron. Output: (a) the hardened script header, (b) the reusable `run-cron.sh` wrapper with logging + flock + failure alert, (c) the corrected crontab line, (d) the `env -i` reproduction command. Bias toward self-sufficient scripts and capturing output yourself rather than trusting cron's defaults.
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 xtrace Debugging and Profiling Prompt
Diagnose and profile a misbehaving Bash script using set -x, custom PS4 with timestamps and line numbers, BASH_XTRACEFD redirection, and targeted tracing — without drowning in noise or leaking secrets.
-
Bash Leveled Logging Library Prompt
Build a small, sourceable Bash logging library with DEBUG/INFO/WARN/ERROR levels, timestamps, TTY-aware color, and a LOG_LEVEL threshold — so your scripts emit consistent, greppable output to stderr.
-
Bash Error Trap and Self-Diagnosing Script Prompt
Add an ERR trap, line-number-aware error reporting, and an opt-in xtrace debug mode to a Bash script so failures print exactly where and why they died instead of a silent non-zero exit.
-
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.