Crontab multiple MAILTO (only error)

CentOS Sep 27, 2017

Recently I had to configure a few cron jobs. Each cron should only send error messages to given email addresses.

Note: I'm handling every cron tasks with one user account.

How to set a global mail address?

To set a mail address for every cron, just edit /etc/crontab

nano /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
 
# 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

As you can see on line 3, you can change the MAILTO variable e.g. to mail@example.com

How to set an individual mail address?

It is also possible to set an individual address for a certain number of cron jobs.

crontab -e
MAILTO="mail1@example.com"
* * * * * [cron 1] > /dev/null
* * * * * [cron 2] > /dev/null
MAILTO="mail2@example.com"
* * * * * [cron 3] > /dev/null
* * * * * [cron 4] > /dev/null

NOTE: crontab only send an email to MAILTO, if an error occurred.


Please comment below, if you have any questions.

Tested on:

  • OS: CentOS 7

Credits:

Tags

Stefan

Howdy! I'm Stefan and I am the main author of this blog. If you want know more, you can check out the 'About me' page.

Impressum | Data Privacy Policy | Disclaimer
Copyright: The content is copyrighted and may not be reproduced on other websites without permission.