Disable cron emails unless there are errors?

The chronic command from moreutils runs a command quietly unless it fails.

Quoting from its manual:

chronic runs a command, and arranges for its standard out and standard error to only be displayed if the command fails (exits nonzero or crashes). If the command succeeds, any extraneous output will be hidden.

A common use for chronic is for running a cron job. Rather than trying to keep the command quiet, and having to deal with mails containing accidental output when it succeeds, and not verbose enough output when it fails, you can just run it verbosely always, and use chronic to hide the successful output.


As you are not caring for the output, you can redirect the STDOUT of a job to /dev/null and let the STDERR being send via mail (using MAILTO environment variable).

So, for example:

...
...
[email protected]
...
...
* * * * * /my/script.sh >/dev/null

will send mail when there is output only on STDERR (with the STDERR), and will discard the STDOUT.

This of course assumes that when a program has written on STDERR, has failed; this might not be always the case. If you have control over the program, you can make it do so. For any complex case, you should write a wrapper of some kind that runs the command(s) and send mail accordingly. And put the wrapper as the cron job.


How can I only receive emails from cron if there are errors?

You could wrap your cron invocations with cronic, a shell script which eats cron output unless the return code of the invoked process is non-zero or there is non-trace error output.

To use cronic, download the script to a suitable location, such as /usr/local/bin. Your crontab entries must be prefixed with the path to the script (e.g. /usr/local/bin/cronic), or simply cronic, provided your PATH is set properly.

Do note that "errors" is an ill-defined term in your question and requires careful definition. For cronic to be useful, you must ensure the jobs you wrap with cronic report errors in one of the ways it defines an error condition. Implicit methods of reporting, such as writing text strings to STDOUT, will require further thought to make this compatible with cronic or another cron reporting mechanism.

Other wrappers are available, as linked from the cronic site:

  • Shush
  • Cronwrap