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.
Table of Contents:
- Why Disable cron emails?
- Method 1: Redirecting Output to Null
- Method 2: Adjusting the MAILTO Variable
- Method 3: Using CRONDARGS for Advanced Control
- Method 4: Error-Only Notifications with Cronic
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:
/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:
Break down team silos, troubleshoot faster, and optimize at enterprise scale.
Get your free product brief
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:
-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:
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: