Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
Thanks very much! +1. Perhaps put the bottom part of your answer in a preformat / code format? –  cwd Dec 16 '11 at 15:44
add comment

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

share|improve this answer
    
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
    
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
    
@cwd sorry to edit your question. Is setting MAILTO in /etc/default/cron working for you? –  Sachin Divekar Dec 16 '11 at 15:46
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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