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.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | cron on Linux/Unix |
Time | 1m |
How To Change Crontab Email Settings ( MAILTO )
The MAILTO 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.
Set MAILTO Variable To Change Crontab Email Setting
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 vivek@nixcraft.in, enter:
MAILTO=vivek@nixcraft.in
Let us send email local user named sai:
MAILTO=sai
If MAILTO is defined but empty (MAILTO=””), no mail will be sent.
MAILTO=""
Otherwise email is sent to the owner of the crontab.
Examples
MAILTO="vivek@nixcraft.in" @midnight /root/scripts/backup-servers 10 20 * * sun /bin/php-cli /wwwroot/apps/cleanup.php # # We can switch email alert too # MAILTO="sysadmin.it@domain-name-here" @daily /root/scripts/push-backup-host.sh MAILTO="storage.admin@domain-name-here" * 10 * * * /root/scripts/check-hardware-raid-array-health.pl # # Disable email alert for rest of jobs # MAILTO="" 5 0 * * * /scripts/bin/daily.sh >> /tmp/daily.shoutput.txt 2>&1
A note about routing email outside of your network or local network
Setting MAILTO will not magically send an email. To send an email, you must have an email server running either on the same host or in your LAN. In other words, Linux and Unix users need an SMTP server like Sendmail or Postfix running. Most modern SMTP allows SMARTHOST where you can forward email using cloud computing or ISP email server. See the following tutorials:
- How to configure Postfix relayhost (smarthost) to send eMail using an external smptd
- How to configure AWS SES with Postfix MTA
Another option is to configure your Gmail account. See
- What is cron on a Linux or Unix-like systems?
- How To Add Jobs To cron Under Linux or UNIX?
- Verify crond Daemon And Cronjobs Are Running
- Start Restart and Stop The Cron or Crond Service
- List / Display All Cron Jobs
- Linux / UNIX Crontab File Location
- Change Crontab Email Settings ( MAILTO )
- Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
- At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
- Execute Cron Job After System Reboot
- Setup and Run PHP Script As A Cron Job
- Run crontab job every minute on a Linux or Unix-like system
- Running crontab (cron jobs) Every 10 Minutes
- Cron Job Script Execution on the Last Day of a Month
- Execute / Run crontab (cron jobs) every 1 minute
- Ubuntu create cron.log file
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 18 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thanks, never even thought it could be changed, I always used my .forward for this. Very handy.
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.
youremail@address-com
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.
Hi All,
Please help me out..
I configured cron job under FTPPKR user.i’ll get mails from FTPPKR@xyz.com whenever cron runs..But whenever i need the mails to be delivered from GTPPKR@xyz.com user.
Is it possibe? If yes, please help me out..
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.
SOLARIS Unix does not support the MAILTO feature. Instead one needs to append the crontab entry with | mailto user@domain.
For example:
00 12 * * * run_command.sh | mail user@domain
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.
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.
@alex: add >/dev/null 2>&1 to the end of your line(s) in crontab. This will redirect all crontab output to /dev/null
What command is cron using to send email? I’m trying to get cron working with esmtp, but no luck so far.
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
Hi..
I am new for cronjob so help me.
How to send a automatically mail to fix mail address .
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.
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
Check your server mail log file. Also, check your spam folder.
Email with errors I receive. So I guess there is no try to send an email.
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
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
Actually, that’s a comment; it starts with a #