A cron job's output fills root's mailbox with thousands of messages. How do you handle cron output properly?
BeginnerScenarioScheduling, automation and scripting
Answer
Situation read → Cron mails any output; redirect deliberately.
What I'd do →
- Send normal output to a log and only errors to mail:
[email protected]
*/10 * * * * app /usr/local/bin/job.sh > /var/log/job.log 2>&1 || echo "job failed"
- Or log to syslog:
job.sh 2>&1 | logger -t job. - Clean the backlog:
> /var/spool/mail/root.
Why → Silent success, loud failure is the right pattern.
Interview tip → Better still: report to a dead-man's switch (Healthchecks.io, Cronitor) so missed runs alert too.
More on Scheduling, automation and scripting
A database dump must run every night at 1:30 AM. How do you schedule it with cron so it is reliable?BeginnerConvert that nightly cron job to a systemd timer. Why would you?IntermediateA cron job runs every 5 minutes, but sometimes takes 8 minutes and two copies overlap, corrupting data. How do you prevent that?BeginnerYou have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?IntermediateA job must run on the last day of every month. Cron has no "last day" field. What do you do?IntermediateYou inherited a server and need to know every scheduled job on it. Where do you look?Beginner