You get a bonus - 1 coin for daily activity. Now you have 1 coin

Turning Off Cron (crontab) Email Notifications for stdout and stderr

Practice



When scripts run on a schedule via cron - since there is no terminal - the cron daemon by default dumps the messages printed by scripts and programs to standard output (i.e., messages to stdout and stderr) into the email of the user under whom the job is run.


For example, suppose your crontab file has a script scheduled to run. And while running, this script prints the line "All Okay :)". What do you think happens? That nothing gets printed anywhere? Not quite — the cron daemon will see this message and send it by email. To whom? To the user under whom the script is run.

That's fine if the message signals that some error occurred. But what if that's not the case and the messages are purely informational? You end up slowly but surely clogging up the local user's mailbox (most often root, since the lion's share of scripts run under it).


Below are a couple of tips on how to turn off these notifications.


Method 1 - kill everything

In this case, all email notifications will be disabled - regardless of which scripts are run.

Add the following line to the /etc/crontab file:
MAILTO=""


Method 2 - disable for specific scripts

* * * * * root /home/myuser/myscript.sh >/dev/null 2>/dev/null

Here, for this particular cron job, we added extra redirects:
  • >/dev/null : redirect all of the script's stdout output to null - i.e., don't print it
  • 2>/dev/null : likewise, send all stderr output to null - i.e., don't print it

That's how you can talk cron out of spamming your mailbox.

Comments

Guest 25-12-2020
Коротко и ясно...

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "LINUX operating system"

Terms: LINUX operating system