Cron is a daemon that executes scheduled commands. More specifically, software utility cron Linux is a time-based job scheduler for Unix-like operating systems. Cron can be used to set up jobs to run periodically at specified times, dates, or intervals.
Cron is an extremely powerful tool because anything you are able to type from the terminal command line can be scheduled using cron. This article lists three common ways to stop cron emails as well as another solution to send emails only when errors occur.
How to deactivate cron email
One of the best, but not the worst, features of cron is the automatic sending of emails. Cron will automatically email the output of your cron jobs. While this can be useful, it can often result in thousands of repeated or duplicate emails. In this case, or for other reasons, you may want to reduce or disable cron email.
Disable cron email using “>/dev/null 2>&1”
We can disable cron email by adding >/dev/null 2>&1
until the end of each cron job line. for example:
0 1 * * * mycommand >/dev/null 2>&1
a quick breakdown >/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 Standard Error
And Standard Out
Getting redirected to /dev/null instead of being sent by email.
mailto = “”
For cron, the default value of MAILTO is root. we can change root
Via MAILTO the value of the variable /etc/crontab
to config file ""
(empty). Example:
MAILTO=""
This disables the cron daemon’s email.
krondargs
If you disable cron email altogether and something goes wrong, you will lose output. You can achieve this by setting CRONDARGS
string. for example:
CRONDARGS= -s -m off
-s
= Forwards the output to the system log.
-m off
= disables cron email.
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 chronic
long term Here’s a small shell script to wrap cron jobs so that cron only sends emails when there is an error. Chronic defines an error as a non-trace error output or a non-zero result code. Example usage:
0 1 * * * cronic mycommand
conclusion
In short, there are a few ways to disable cron email. However, unless you want to turn off email entirely, I’d recommend starting with Chronic first. If you instead forward the cron output to the system log, be careful not to flood the log by doing too many 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
Leave a Comment