/etc/cron.daily/foo : Send email to a particular user instead of root?

I’m running CentOS 5.5.

We have several cronjobs stored in /etc/cron.daily/ . We would like the email for some of these cronjobs to go to a particular email address, while the rest of the emails in /etc/cron.daily/ should go to the default email address (root@localhost).

Cronjobs in /etc/cron.daily/ are run from the /etc/crontab file. /etc/crontab specifies a ‘MAILTO’ field. Can I override this by setting MAILTO in my /etc/cron.daily/foo cronjob?

What’s the best way to handle this?

Asked By: Stefan Lasiewski


Setting MAILTO=user@example.org in /etc/cron.daily/foo does not work. The script output is not sent to user@example.org .

The page at http://www.unixgeeks.org/security/newbie/unix/cron-1.html also suggests a simple solution:

The file /etc/cron.daily/foo now contains the following:

/usr/bin/script 2>&1 | mailx -s "$0" stefanl@example.org

This will send an email to ‘stefanl@example.org’ with the subject which is equal to the full path of the script (e.g. /etc/cron.daily/foo).

Here’s what Unixgeeks.org says about this:

Output from cron

As I’ve said before, the output from
cron gets mailed to the owner of the
process, or the person specified in
the MAILTO variable, but what if you
don’t want that? If you want to mail
the output to someone else, you can
just pipe the output to the command
mail. e.g.

cmd | mail -s “Subject of mail” user

Sometimes, I only want to receive the errors from a cronjob, not the stdout, so I use this trick. The syntax may look wrong at first glance, but rest assured it works. The following cronjob will send STDOUT to /dev/null, and will then handle STDERR via the pipeline.

doit 2>&1 >/dev/null | mailx -s "$0" stefanl@example.org

Same thing, but send to syslog:

doit 2>&1 >/dev/null | /usr/bin/logger -t $ME

Also see my answer on ServerFault to Cronjob stderr to file and email

Answered By: Stefan Lasiewski

Assuming you have SA access on this machine, you can create a new user account, add the tasks to this accounts cron tasks. The mail for this user can then be forwarded using a .forward file in this accounts home folder. You may have to set up permisions for this user if the cron tasks require privilaged access.

Whether this or Stefan’s answer suits best really depends on how much hassle you want in setting it up and whether you want error messages to go to the root email or to the people that normally monitor the daily output.

Good luck

Answered By: Michael Shaw

A more elegant solution would be to use /etc/cron.d directly. Instead of having your script in /etc/cron.daily, put it somewhere like /usr/local/sbin/myscript.sh and then create the file /etc/cron.d/myscript as:

# run myscript.sh at 4:11 every day
11 4 * * * root /usr/local/sbin/myscript.sh

This also gives you a lot more control over when the job happens; for example only on certain weekdays etc. See the crontab(5) man for more information.

Answered By: codebeard
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.