The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 Only Email On Cron Errors For Jobs In Cron.Daily, Cron.Hourly, Etc
00:46 Answer 1 Score 4
01:10 Accepted Answer Score 6
01:35 Answer 3 Score 1
02:18 Thank you

--

Full question
https://superuser.com/questions/290054/o...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #email #cron #crontab #cronjob

#avk47



ACCEPTED ANSWER

Score 6


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.




ANSWER 2

Score 4


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




ANSWER 3

Score 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).