Linux / UNIX: Change Crontab Email Settings ( MAILTO )

Posted on in Categories , , last updated May 3, 2017

I would like to send an email to [email protected] instead of default system root user for crond service on a Linux or Unix-like system. How do I change the email settings under crontab file on a Linux or Unix-like systems?

A crontab file contains instructions to the cron daemon. It wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab or to the user named in the MAILTO environment variable in the crontab, if such exists. The syntax is:

name = value

Where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks. The value string is not parsed for environmental substitutions, thus lines like:

PATH = $HOME/bin:$PATH

will not work as you might expect.

MAILTO Variable

In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a result of running commands in “this” crontab. If MAILTO is defined (and non-empty), mail is sent to the user so named. First open your crontab file:
# vi /etc/crontab
OR
$ crontab -e
To send email to [email protected], enter:
[email protected]

If MAILTO is defined but empty (MAILTO=””), no mail will be sent.
MAILTO=""
Otherwise mail is sent to the owner of the crontab.

This entry is 7 of 15 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
  1. What is cron on a Linux or Unix-like systems?
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. Linux Verify crond Daemon And Cronjobs Are Running
  4. Linux Start Restart and Stop The Cron or Crond Service
  5. Linux: List / Display All Cron Jobs
  6. Linux / UNIX Crontab File Location
  7. Linux / UNIX: Change Crontab Email Settings ( MAILTO )
  8. Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
  9. Linux: At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
  10. Linux Execute Cron Job After System Reboot
  11. Linux / UNIX Setup and Run PHP Script As A Cron Job
  12. How to run crontab job every minute on a Linux or Unix-like system
  13. Run crontab (cron jobs) Every 10 Minutes
  14. Cron Job Script Execution on the Last Day of a Month
  15. Run crontab (cron jobs) every 1 minute

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

18 comment

  1. I too have always the “.forward” option. This is done by the follwing…..

    create a new file called .forward
    vi /root/.forward

    Enter the email address on the first line in the file.
    [email protected]

    Save (w) the file and exit (q)
    :wq

    Now, anytime the ‘root’ user get an email, it will automatically be forwarded to the address you specified in the .forward file.

    You can also place the .forward file in users home directories /home/user1/.forward

    Thanks for the info up above though, I’ll have to check that out.

  2. Yes, that’s entirely possible. Just figure out if the emails are being sent with sendmail or postfix, then adjust their settings to reflect that. You could deploy address rewrites to achieve this, for example.

  3. SOLARIS does not support the MAILTO feature. Instead one needs to append the crontab entry with | mailto [email protected]
    Ex:
    00 12 * * * run_command.sh | mail [email protected]

  4. Mail addressed to ‘root’ is often redirected to some other account using /etc/aliases. You might like to edit (or add) the line beginning with ‘root:’ to point to some other account.

  5. But, How if I dont want to receive notification from crontab? How to set it ? Because I’m monitoring mail server, and notification from crontab unnecessary for me.

  6. @alex: add >/dev/null 2>&1 to the end of your line(s) in crontab. This will redirect all crontab output to /dev/null

  7. What command is cron using to send email? I’m trying to get cron working with esmtp, but no luck so far.

  8. I wanted to know under what circumstances will the email be sent.
    Is it only when cron encounters a warning or error
    Or even when the commad is executed succesfully

  9. Hi..
    I am new for cronjob so help me.
    How to send a automatically mail to fix mail address .

  10. Hi,

    While using cronjob when my cronjob fails then the mail is sent to the user who is login but not to mailto user. What is required to send mail to the “mailto” user even when the test fails.

  11. Hi!
    I’ve setted up a new cron (rsync job), the first few days it mades errors which I get by email. Now it looks to work fine, but I don’t get an email. Someone knows why?

    thanks in advance

  12. i have this line in crontab could someone explain me what is the mean of this?
    thanks by advance
    #*/1 * * * * root /usr/local/rtm/bin/rtm 26 > /dev/null 2> /dev/null

    1. this will run the /usr/local/rtm/bin/rtm script once every minute and redirects the output messages to /dev/null so as to get rid of them

Comments are closed.