13

I believe that if there is any output from a cronjob it is mailed to the user who the job belongs to. I think you can also add something like MAILTO=vivek@nixcraft.in at the top of the cron file to change where the output is sent to.

Can I set an option so that cron jobs system-wide will be emailed to root instead of to the user who runs them? (i.e. so that I don't have to set this in each user's cron file)

9

Check /etc/crontab file and set MAILTO=root in there. Might also need in /etc/rc file

crond seems to accept MAILTO variable, I guess I am not sure completely but its worth a try changing the environment variable for crond before it is started. Like in /etc/sysconfig/crond or /etc/rc.d/init.d/crond script which sources the earlier file.

Example:

[centos@centos scripts]$ strings /usr/sbin/crond | grep -i mail
ValidateMailRcpts
MailCmd
cron_default_mail_charset
usage:  %s [-n] [-p] [-m <mail command>] [-x [
CRON_VALIDATE_MAILRCPTS
mailed %d byte%s of output but got status 0x%04x
[%ld] no more grandchildren--mail written?
MAILTO
/usr/sbin/sendmail
mailcmd too long
[%ld] closing pipe to mail
MAIL
  • Thanks very much! +1. Perhaps put the bottom part of your answer in a preformat / code format? – cwd Dec 16 '11 at 15:44
9

Put MAILTO=root in /etc/default/cron. This file contains cron's default settings.

man 5 crontab says,

If MAILTO is defined (and non-empty), mail is sent to the user so named.

Refer: http://man.cx/cron(1)#heading4

  • where? for each user's crontab file? – Nikhil Mulley Dec 16 '11 at 15:13
  • @Nikhil check my updated answer. – Sachin Divekar Dec 16 '11 at 15:25
  • 1
    it works for only the users' whose crontab has it. Will it work, if any of the users' do not have this variable set? I think @Cwd has asked specifically for a global setting that for all users. – Nikhil Mulley Dec 16 '11 at 15:29
  • @SachinDivekar -1 Nikhil is right. Also you edited my question and changed the meaning of it. The edited meaning does align with your answer, but this is not what i was asking. – cwd Dec 16 '11 at 15:43
  • 2
    This is not working for me. I have set the MAILTO in the default file and restarted cron service, but it's still sending to root (I'm trying to send to an external email address) – peedee Nov 18 '15 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.