67

In the beginning of a crontab file you could use the MAILTO instruction to indicate you want the output to be sent as an e-mail to an e-mail address. I would like to send the output to multiple addresses. Is it possible (and how) to specify multiple addresses?

030
  • 5,731
  • 12
  • 61
  • 107
Boaz
  • 2,159
  • 5
  • 19
  • 15

5 Answers5

90

It may differ depending exactly which cron daemon package you use, but this is from the manpage of Vixie Cron on Ubuntu Hardy:

If MAILTO is defined (and non-empty), mail is sent to the user so named. MAILTO may also be used to direct mail to multiple recipients by separating recipient users with a comma. If MAILTO is defined but empty (MAILTO=""), no mail will be sent. Otherwise mail is sent to the owner of the crontab.

If you're not using Vixie Cron, or aren't sure, try the manual page for the crontab file: man 5 crontab

Example

MAILTO="user.one@domain.one,user.two@domain.two"
030
  • 5,731
  • 12
  • 61
  • 107
ThatGraemeGuy
  • 15,314
  • 12
  • 51
  • 78
  • 17
    One thing to note -- use _just_ a comma between addresses. *Do not use a space as well*, otherwise cron will log the address (probably in /var/log/cron or /var/log/daemon) as "UNSAFE" and will refuse to send to that list of addresses. – David Gardner Sep 06 '13 at 08:23
  • 2
    When separating addresses with spaces _instead_ of commas, it happened to me that `cron` sent mails to the first address _only_ (and `crontab` had not complained when saving changes, which is a _shame_). There are errors in the syslog (`(CRON) error (bad mailto)`), but nobody noticed ... – Tobias Sep 11 '13 at 08:52
12

As an alternative to the above answers, you can send email to a single address which is a mail group or distribution list. This works especially well if you manage multiple servers since it is easier to manage addresses on your mail server rather than in crontab of each individual machine.

dtoubelis
  • 4,579
  • 1
  • 28
  • 31
  • IMHO this is the right answer. Who wants to maintain individual lists of email addresses in various crontabs? – Satanicpuppy Feb 03 '14 at 16:49
  • I created a simple list: 1. As root, add a "virtual recipient" to `/etc/aliases` by adding a line like this: `cron-listeners: fred@example.com, george@example.com` 2. In the crontab, set `MAILTO=cron-listeners` If you don't have root access, then you could try the `.forward` method instead. – joeytwiddle Jul 17 '17 at 02:29
5

One solution might be a .forward file on that user account which sends mail to the appropriate addresses.

chicks
  • 3,639
  • 10
  • 26
  • 36
Matt Simmons
  • 20,218
  • 10
  • 67
  • 114
2

The best answer is probably to either check your man pages for your distro's current version of cron/crontab to see if there's something that can be done with the MAILTO parameter or specify a mail address that in turn forwards mail to multiple addresses for you.

There are often two crontab man pages, the configuration file is typically in section 5, so use man -s 5 crontab.

gerard
  • 243
  • 1
  • 6
Bart Silverstrim
  • 31,092
  • 9
  • 65
  • 87
0

Add , after each email, e.g.: MAILTO="some.user1@example.com,some.user2@example.com"

dannyb2100
  • 17
  • 2