Hardening Production Linux Crontab Jobs Against Silent Failures
Linux cron daemons (Vixie cron on Debian/Ubuntu, cronie on RHEL/Rocky) execute jobs in a bare environment where PATH is often just /usr/bin:/bin and SHELL defaults to /bin/sh rather than bash. Defining SHELL=/bin/bash, PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, and MAILTO="" at the top of your crontab prevents "command not found" failures and runaway local mail spools.
Two classic Linux crontab gotchas catch even senior admins: unescaped % characters inside date format strings are treated as newline/stdin breaks by cron (escape as \%), and the crontab file must end with a trailing newline or cron may ignore the last entry.
- User vs System FileUser crontab -e has 5 time fields + command; /etc/crontab and /etc/cron.d/* add a 6th username column before the command.
- Percent % TrapUnescaped % signs in crontab commands are converted to newlines; always escape them as \% (e.g., date +\%F).
- Overlap ProtectionWrap long-running commands with flock -n /tmp/job.lock to prevent concurrent pileups on frequent intervals.
Why does my date +%Y-%m-%d command work in bash but fail inside crontab?
In Vixie cron and cronie, unescaped % characters are replaced with newline characters, and everything after the first % is fed to standard input. Escape every % as \% in crontab.
What is the difference between crontab -e and /etc/cron.d/ files?
Files in /etc/cron.d/ require a username field between the 5 schedule fields and the command, must be owned by root, and cannot have dots in their filename on Debian systems.