22

We have a role account at work that has a pretty big crontab. Its MAILTO is pointed at a shared address, so that a number of us get notified if something fails.

I'd like to add an entry to this crontab, but I only want myself to be notified if something goes wrong. Is there a way to change MAILTO for this one entry, or otherwise accomplish my goal?

1
  • Another approach to those suggested so far, is to use the feature that cron emails the user who is running the job, so make another user just for this job, and forward its emails to you. Apr 25, 2014 at 3:50

3 Answers 3

22

You can always just do:

MAILTO=you
* * * ...  your cron job
MAILTO=normal.destination
3
  • 1
    Did the people who upvoted this really test it out? If so, what cron implementation? Sep 26, 2012 at 6:57
  • 1
    I tested it with the ICS/Vixie cron that is shipped with Ubuntu 14.04 ("3.0pl1-124ubuntu2"), and it works well there. Nov 26, 2015 at 8:54
  • Ugly, but pretty much only way to sort this out. Works!
    – stamster
    May 7, 2018 at 8:37
10

You can always pipe all output to the mail command with the correct address in a subshell. As long as there is nothing on STDOUT or STDERR cron will not send the email

10 * * * * sh -c 'thisonecommand 2>&1 | mail [email protected]'

2
  • 1
    This doesn't work -- the "mail" command sends regardless of whether or not it gets output. So I get an empty message on success, whereas what I want is no message on success.
    – mike
    Jul 1, 2009 at 17:46
  • 5
    Nice! And to avoid empty emails from mail command, use this parameter: -e (or -E for some..)
    – user58599
    Oct 28, 2010 at 20:17
4

I can't recall if cron preparses the file to read in the environment vars, so I'm not sure if you can change MAILTO multiple times in a single file. But you can always split it to another file and put it in /etc/cron.d/foo with a [email protected].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .