Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Better to fix the script than to prevent cron from sending you the output. The next error might not be so trivial, and you don't want to miss it. –  Wyzard Jul 20 '12 at 2:43

4 Answers 4

up vote 4 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.

share|improve this answer

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

share|improve this answer
1  
+1. MAILTO="" also works, if I recall correctly. –  natxo asenjo Sep 28 '10 at 10:26
    
this is my favorite way as it's more "readable" –  Mike Jul 20 '12 at 2:31
    
It needs to be noted that the MAILTO environment variable is NOT supported in Solaris 10, so those looking for a fix in that environment should use the accepted answer instead. –  Marc Reside Sep 25 '12 at 14:50

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.

share|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

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

e.g.

/directory/script.sh > /dev/null 2>&1 &
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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