5

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.

  • 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 02:43

4 Answers4

6

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

theotherreceive
  • 8,365
  • 1
  • 31
  • 44
5

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.

wzzrd
  • 10,409
  • 2
  • 35
  • 47
2

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.

Oneiroi
  • 2,063
  • 1
  • 15
  • 28
  • 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 05 '10 at 05:02
0

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

e.g.

/directory/script.sh > /dev/null 2>&1 &
Scott Pack
  • 14,907
  • 10
  • 53
  • 83
Mark Regensberg
  • 1,411
  • 12
  • 14