I have several cron jobs that run (in /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, etc.) and email root@localhost with the results. I'd like to stop those emails if the jobs are succeeding, and only email on error (which I understand can be done by redirecting stdout to /dev/null). I understand how to do that for individual cron jobs, but the scripts in those special directories are run using run-parts. What is the best way to suppress success emails for those scripts?
- 13,195
3 Answers
You may want to use one of the wrappers for the programs, that output everything when something goes bad and swallow stdout otherwise.
One example might be cronic, just prepend 'cronic' to 'run-parts' e.g.:
# m h dom mon dow user command
17 * * * * root cd / && /etc/cronic run-parts --report /etc/cron.hourly
where /etc/cronic is a place with executable cronic script, downloaded from the website mentioned.
- 178
You should send successful email notifications to /dev/null so they disappear.
But you want to see unsuccessful email notifications.
This means you need to first direct stdout to /dev/null and then direct /dev/stderr to stdout
try changing the redirection part of your cronjobs to
>/dev/null 2>&1
See this link
- 6,490
- If the script is well behaved, it will write only to
STDOUTif successful, and toSTDERRin case there is an error. - By default, cron will mail everything that the script writes into
STDOUTorSTDERR(Arch wiki).
So, if you want to keep error notifications, don't redirect STDERR, just STDOUT:
COMMAND > /dev/null
If you do the typical >/dev/null 2>&1, you are effectively suppressing both (bash documentation).
- Make
stdinfile descriptor a copy of /dev/null. - Make
stderrfile descriptor a copy ofstdout(that already pointed to /dev/null).
- 111