Today i setup ssmtp to email me from sendmail. However what i didnt expect is that applications like cron would email me.

One of my cron scripts before backing up the db tries to create path/year/month/ and since year and month already exist i get the email

From: Root
Subject: Cron <root@myserver> /path/scripts/mysqlbackup.sh
mkdir: cannot create directory `2010': File exists
mkdir: cannot create directory `09': File exists

How do i make it stop? Its only been up for an hour so if this is the only error i'll simply just fix the script.

link|improve this question

50% accept rate
feedback

4 Answers

up vote 1 down vote accepted

By default, cron mails you the output messages of a cronjob. More specifically, it saves stdout (standard output) and stderr (standard error output) of a cronjob and emails it the owner of the crontab, or the user named in the MAILTO variable in the crontab.

To prevent output reaching you, you have to make sure that stdout and stderr is not saved, but written to a file or /dev/null.

You can do this by using something like this in your job definition:

job.sh > /dev/null 2>&1

This redirects stdout to /dev/null (the '> /dev/null' part) and redirects stderr to stdout, and thus to /dev/null, too (the '2>&1' part). Odd syntax, right? ;-)

The Advanced Bash Scripting Guide has a lot more information about I/O redirection.

Alternatively, you can leave MAILTO empty (that is, define MAILTO="" in your crontab) and you will not get any mail, according to vixie-cron's manpage on RHEL4 and RHEL5.

link|improve this answer
feedback

As an alternative to redirecting the script output you can change the mail destination for a crontab file by setting MAILTO, eg. MAILTO=nobody

link|improve this answer
+1. MAILTO="" also works, if I recall correctly. – natxo asenjo Sep 28 '10 at 10:26
feedback

Fix the problem not the notification,

It's going to get you into a bad habit of just sending all notifications to >/dev/null, and sods law being what it is, you will miss something important.

link|improve this answer
1  
This is the better answer, with the additional comment that in the script if you changed the mkdir command to a "mkdir -p $YEAR/$MONTH" type of command, mkdir will not complain about paths that already exist AND will make them all for you at the same time. – unixguy Oct 5 '10 at 5:02
feedback

append > /dev/null 2>&1 & to the end of the command

e.g.

/directory/script.sh > /dev/null 2>&1 &

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.