How to change crontab email alert

By default, crontab mails are sent to “root” user. If you want to get those mail to particular user, it is also possible in linux. This can be done by two methods. Crontab configuration file is located at “/etc/crontab”

Default crontab configuration file settings.

Now, i’m going to open the crontab configuration file using nano txt editor.

[email protected] [~]# nano /etc/crontab
  GNU nano 2.0.9                                          File: /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

The output is clearly shows all the crontab mails sent to “root” user.

Method-1 : How to Change Crontab email alert id using MAILTO option.

For example,I want to receive all the mails to [email protected] Then what we have to do for that is,just replace [email protected] email id instead of root on “/etc/crontab” file and See the output.

[email protected] [~]# nano /etc/crontab
  GNU nano 2.0.9                                          File: /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=[email protected]
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

Method-2 : How to Change Crontab email alert id using “.forward” file.

For example,I want to receive all the mails to [email protected], then to get it so,just create a new file called (dot).forward under “/root” and add the [email protected] email id.

[email protected] [~]# nano .forward
  GNU nano 2.0.9                                           File: .forward

[email protected]

3) How to stop/ban particular cronjob mails.

For example, if you run the cron every 5 mins and you will be getting email alert frequently. So if you want to stop/ban that cronjob mails. just add the this line at end of the cronjob “>/dev/null 2>&1”. See the output of my cronfile entry.

[[email protected] ~]# crontab -e
0 18 * * * /bin/mysql_backup.sh
0 20 * * * /bin/website_backup.sh
0 12 * * * /bin/offsite-backup.sh
0 21 * * * /bin/offdb-backup.sh
*/5 * * * * /usr/local/scripts/autorepair recoverymgmt >/dev/null 2>&1

We haven’t get any mail alert of my last cronjob.

4) How to stop/ban all cronjob mails.

If you defined MAILTO=”” (empty), you haven’t received any mails from crontab.

[email protected] [~]# nano /etc/crontab
  GNU nano 2.0.9                                          File: /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

Go through this link to get more details about cronjob and practical examples.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...