How to Disable Cron Emails

Introduction

Last week I set up an email server using Postfix (to send and receive mail) and Dovecot (to integrate with my mail client). I also use cron to run scheduled tasks. Shortly after setting up my email server, I began receiving emails from the Cron Daemon.

In this brief article, I’ll show you how to disable emails globally and for a particular task.

Resolution

It turns out that by default, “any output is mailed to the owner of the crontab (or to the user specified in the MAILTO environment variable in the crontab, if such exists)” per the documentation.

To globally disable receiving emails, you can set the MAILTO environment variable in the crontab to an empty string: MAILTO="".

For my particular case, I didn’t want to disable this behavior globally but only for one specific task. I did this by redirecting all output to /dev/null (>/dev/null 2>&1). Here’s a concrete example:

59 23 * * * goaccess /var/log/nginx/access.log -o /var/www/website_stats/index.html --log-format=COMBINED >/dev/null 2>&1

Rather than disgarding the output, you could also redirect it to a log file.

59 23 * * * goaccess /var/log/nginx/access.log -o /var/www/website_stats/index.html --log-format=COMBINED >> ~/logs/foo.log 2>&1

Side note: > will overwrite the content in ~/logs/foo.log; whereas, >> will append the content to the end of the file.


linux

213 Words

2022-04-26 20:04 -0400