This article lists three common methods to disable cron emails and another solution to send emails only when errors occur. 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.
Table of Contents
Why Disable cron emails?
Cron’s automatic email feature is a double-edged sword. While it conveniently sends outputs of cron jobs via email, it can lead to an overwhelming influx of repetitive or duplicate messages. This can clutter your inbox, making it challenging to identify important emails.
To manage this, you might consider either reducing the frequency of these notifications or disabling them entirely. This approach helps streamline your email management, ensuring that only essential communications capture your attention, which is particularly beneficial in a busy IT environment.
Method 1: Redirecting Output to Null
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.
Method 2: Adjusting the MAILTO Variable
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.
Method 3: Using CRONDARGS for Advanced Control
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
Method 4: Error-Only Notifications with 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
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.
Managing cron emails effectively enhances your system’s efficiency and keeps your inbox clutter-free. Whether you choose to disable these emails entirely or selectively reduce their frequency, the methods outlined offer practical solutions tailored to your specific needs. Ultimately, the right approach depends on your unique operational requirements and preferences, ensuring that you receive only the most relevant notifications.
Published: Jun 10, 2020 | Last update: December 21, 2023