Stop CRON from sending email for each job

To totally unlock this section you need to Log-in


Login

Scenario

I have some cron jobs that run overnight on my Linux systems. Each of these jobs output information to a text file if I ever need to review. Some are written to send emails via the mail command. But since I put these scripts on a new system and added them to crontab, I am getting an email for each job that runs. There are too maybe emails being sent to root. Is there a way to stop this behavior?

Solution

Crond typically sends an email when a cron job is run. It uses the MAILTO variable in /etc/crontab to determine who receives the email, by default this is root. There are several ways to stop this behavior.

Change the MAILTO variable to blank

You can edit the /etc/crontab file and change the MAILTO variable to the following:

MAILTO=””

This will effectively disable all emails from the cron daemon. You can then decide from within the script to send mail using the mailx command or the command of your choice.

This is not our preferred method as we like to receive an email when there is an error with the cronjob, but sometimes this approach could be helpful.

Redirect STDOUT and STDERR to null to suppress output

By suppressing output of the script, there will be nothing for crond to send. Add the following to the crontab entry to send all output (STDERR and STDOUT) to the /dev/null.

>/dev/null 2>&1

For example:

0 5 * * * /example/script >/dev/null 2>&1

This also has it’s drawbacks as you will be suppressing any errors that may be helpful to debug problems with the script.

Configure crond to send the script output to the system log, and disable sending mail of output

You can configure crond by editing the /etc/sysconfig/crond file and changing the CRONDARGS line. Adding the “-s” argument will send the output to the system log, and adding the “-m off” argument will disable crond from sending emails of the job output.

For example:

[[email protected] ~]# cat /etc/sysconfig/crond
# Settings for the CRON daemon.
# CRONDARGS= : any extra command-line startup arguments for crond
CRONDARGS=-s -m off

You will have to restart the crond service to read the new arguments:

systemctl restart crond.service

Any of the above methods will work for completely suppressing emails from cron daemon when jobs run. This is not ideal in our mind as we would like to be notified if errors occur in our cron jobs.

We prefer to either write our scripts to produce no output (no standard output-but still output errors), or redirect STDOUT only to /dev/null. This will cause crond to ONLY send an email if an error has occurred.

Example of only redirecting STDOUT only:

0 5 * * * /example/script > /dev/null

Category