Cron is a daemon that executes scheduled commands. More specifically, the software utility cron is a time-based job scheduler for Unix-like operating systems, like Linux. Cron can be used to set up jobs to run periodically at fixed times, dates, or intervals.
Cron is an extremely powerful tool because just about anything that you are able to type from terminal command line, can be scheduled using cron. This article list three common methods to stop cron emails as well as another solution to send email only when errors occur.
Methods to disable cron emails
One of the best, but also worst features of cron is the automatic sending of emails. Cron will automatically email the output of your cron jobs. Although this can be useful, it can often result in thousands of repeat or duplicate emails. In this case, or for other reasons, you may want to reduce or disable cron emails.
Disable cron emails using “>/dev/null 2>&1”
We can disable cron emails by adding
>/dev/null 2>&1 to the end of each cron job line. For example:
0 1 * * * mycommand >/dev/null 2>&1
A quick breakdown of
> = redirect.
/dev/null = a device file location in Unix systems that discards any data written to it.
2>&1 = redirects stderr (standard errors) and stdout (standard output).
This results in both the
Standard Error and
Standard Out being redirected to /dev/null, rather than sent by email.
For cron, the default value of MAILTO is root. We can change the
root value of the MAILTO variable via the
/etc/crontab config file to
"" (blank). Example:
This disables cron daemon’s emails.
If you disable cron emails completely and something goes wrong, you will lose the output. You can get around this by setting
CRONDARGS string. For example:
CRONDARGS= -s -m off
-s = forwards the output to system log.
-m off = disables cron emails.
On RHEL/Fedora you can edit
/etc/sysconfig/crond. So that it looks similar to:
# Settings for the CRON daemon. # CRONDARGS= : any extra command-line startup arguments for crond CRONDARGS= -s -m off
For Debian/Ubuntu you can make edit using:
systemctl edit --full cron.service
Send errors-only using Cronic
Cronic is a small shell script for wrapping cron jobs so that cron will only send email when an error has occurs. Cronic defines an error as any non-trace error output or a non-zero result code. Example usage:
0 1 * * * cronic mycommand
In summary, there are a couple of ways to disable cron emails. However, unless you want to completely stop emails, I would recommend starting first with cronic. If you do forward cron output to system logs instead, be careful not to flood logs by creating very frequent writes to your log file.