Only email on cron errors for jobs in cron.daily, cron.hourly, etc

8

2

I have several cron jobs that run (in /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, etc.) and email root@localhost with the results. I'd like to stop those emails if the jobs are succeeding, and only email on error (which I understand can be done by redirecting stdout to /dev/null). I understand how to do that for individual cron jobs, but the scripts in those special directories are run using run-parts. What is the best way to suppress success emails for those scripts?

jrdioko

Posted 2011-05-29T01:15:55.877

Reputation: 8 205

Answers

5

You may want to use one of the wrappers for the programs, that output everything when something goes bad and swallow stdout otherwise.

One example might be cronic, just prepend 'cronic' to 'run-parts' e.g.:

# m h dom mon dow user  command
 17 *  *   *   *  root  cd / && /etc/cronic run-parts --report /etc/cron.hourly

where /etc/cronic is a place with executable cronic script, downloaded from the website mentioned.

Cyryl Płotnicki

Posted 2011-05-29T01:15:55.877

Reputation: 168

5

You should send successful email notifications to /dev/null so they disappear.

But you want to see unsuccessful email notifications.

This means you need to first direct stdout to /dev/null and then direct /dev/stderr to stdout

try changing the redirection part of your cronjobs to

>/dev/null 2>&1

See this link

pavium

Posted 2011-05-29T01:15:55.877

Reputation: 5 956

Wouldn't that suppress the error emails too (because they wouldn't produce any output)? Also, I need to do this for my cron.XXX directories which use run-parts, so it's not as simple as redirecting for individual scripts. – jrdioko – 2011-05-29T01:47:20.143

No the idea is that stderr is thrown away then stderr is redirected to stdout. I'm not sure what run-parts is, but however it works redirection of stdout and then stderr seems to be the way. – pavium – 2011-05-29T02:01:50.637

Aha, I googled run-parts. That does complicate the issue, doesn't it. Maybe you should avoid run-parts and invoke each script separately. – pavium – 2011-05-29T02:05:21.960

Ah ok, I understand. run-parts runs all scripts in directories like /etc/cron.daily, so the trick is passing along the redirection to the individual scripts it is running. – jrdioko – 2011-05-29T02:06:30.150

I think so. I couldn't find anything about redirecting all output from run-parts (not even in the man page I just discovered I have. – pavium – 2011-05-29T02:14:37.980

2Are you sure about the >/dev/null 2>&1 bit? I tested it and that funnels everything to /dev/null, where if you drop the 2>&1 only stdout gets removed. – jrdioko – 2011-05-29T04:42:53.813

1

  • If the script is well behaved, it will write only to STDOUT if successful, and to STDERR in case there is an error.
  • By default, cron will mail everything that the script writes into STDOUT or STDERR (Arch wiki).

So, if you want to keep error notifications, don't redirect STDERR, just STDOUT:

COMMAND > /dev/null

If you do the typical >/dev/null 2>&1, you are effectively suppressing both (bash documentation).

  1. Make stdin file descriptor a copy of /dev/null.
  2. Make stderr file descriptor a copy of stdout (that already pointed to /dev/null).

morallo

Posted 2011-05-29T01:15:55.877

Reputation: 11

Even if this does not fully answer the original question, it addresses some errors in pavium's reply.

– morallo – 2015-09-02T09:15:32.957