Disable cron emails (solution)
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. You can use Cron to set up jobs to run periodically at fixed times, dates, or intervals.
Cron is an extremely powerful tool because just about anything you can type from the command line can be scheduled using cron. This article lists three common methods to stop cron emails and another solution to send emails 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 >/dev/null 2>&1
:
>
= 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.
mailto=””
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:
MAILTO=""
This disables cron daemon’s emails.
CRONDARGS
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 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 an email when an error has occurred. Cronic defines an error as any non-trace error output or a non-zero result code. Example usage:
0 1 * * * cronic mycommand
Summary
There are a couple of ways to disable cron emails. However, unless you want to stop emails completely, I recommend starting with cronic. If you forward cron output to system logs instead, be careful not to flood logs by creating very frequent writes to your log file.
Related reading:
Enable Automatic Updates – Fedora/Red Hat/CentOS + Bonus Tip
How to Enable Unattended Upgrades on Ubuntu/Debian
Published: Jun 10, 2020 | Last update: May 31, 2022
couldn’t you use cron -L and follow the instructions in crontab (1 & 5) man pages?
And Debian uses it’s own version , See cron (8) man page “Debian Specific” section:
DEBIAN SPECIFIC
Debian introduces some changes to cron that were not originally avail-
able upstream. The most significant changes introduced are:
Thanks for sharing this @tmick