All the major BSDs support a -n prefix before the command that only mails if the command fails. From the OpenBSD documentation:
-n command
No mail is sent after a successful run. The execution output
will only be mailed if the command exits with a non-zero exit
code. The -n option is an attempt to cure potentially copious
volumes of mail coming from cron(8).
For some reason this doesn't seem to have made its way to Vixie Cron and its descendants, including cronie.
It seems like a common enough question on Stack Overflow, and people have all kinds of elaborate workarounds in the replies:
If the command exits with success (exit code 0), no email is sent
If the command exits with failure (exit code non-zero), an email is sent containing stdout and stderr
NOT ALTERNATIVE SOLUTIONS:
cmd >/dev/null 2>/dev/null: This blindly suppresses all failures
cmd >/dev/null: Better because at least you get error output, but if the job fails you lose potentially useful information that was in stdout. Exit code is not checked
cmd || echo job failed: Requires modification of cmd to suppress normal output, which may be difficult or impossible, and potentially lose valuable contextual information as to why the command failed
All the major BSDs support a -n prefix before the command that only mails if the command fails. From the OpenBSD documentation:
For some reason this doesn't seem to have made its way to Vixie Cron and its descendants, including cronie.
It seems like a common enough question on Stack Overflow, and people have all kinds of elaborate workarounds in the replies:
TO CLARIFY: the desired behavior here is:
NOT ALTERNATIVE SOLUTIONS:
cmd >/dev/null 2>/dev/null
: This blindly suppresses all failurescmd >/dev/null
: Better because at least you get error output, but if the job fails you lose potentially useful information that was in stdout. Exit code is not checkedcmd || echo job failed
: Requires modification ofcmd
to suppress normal output, which may be difficult or impossible, and potentially lose valuable contextual information as to why the command failed