Crontab MAILTO Parameter to Send Notification

Crontab is a Linux utility utilized to schedule tasks at specific dates or times. The “MAILTO” parameter in crontab is used to specify a user’s email address so that cron can send an email notification when a job fails. The “MAILTO” parameter helps automate system maintenance tasks, such as backups and email notifications.

This article will illustrate the step-by-step procedure to send notifications using the “MAILTO” parameter in crontab.

How to Set a MAILTO Parameter to Send Notification in Crontab?

To send notifications using the “MAILTO” parameter in Crontab on Linux, you need to follow the steps below:

Prerequisite Step: Check Cron Services

Before modifying the crontab file, it is necessary to check the cron services via the command:

$ sudo systemctl status cron

The output shows that cron services are in an active state.

Step 1: Open Crontab File

Open/Create the crontab file using the “crontab” command with the “-e” option:

$ crontab -e

Upon execution, you need to choose the editor to be used for opening the crontab file:

Step 2: Set the “MAILTO” Parameter

It navigates to the “crontab” configuration file in the nano editor. At there, set the “MAILTO” parameter by assigning the mail such as “[email protected]” and specify the script file “mail.sh” in which content is written to send a notification:

$ [email protected]
$  * * * * * /home/itslinuxfoss/mail.sh

After adding the lines to the crontab file, press “Ctrl+S” and “Ctrl+X” to save and exit the file.

Note: To send the notification, the user must specify the mail address and write a custom message in the “.sh” file.

Step 3: Verify the Sent Notification

To verify the notifications, the “grep” command is utilized with the “a” option by specifying the script file. For instance, specify the “mail.sh” that is located in the “/var/log/syslog” directory:

$ sudo grep -a "mail.sh" /var/log/syslog

The output shows the sending notifications at each second in the terminal.

Conclusion

In Linux, the crontab offers the “MAILTO” parameter to set the specified mail to send notifications. Once the job is executed, an email will be forwarded to the specified address. This article has briefly explained the step-by-step instructions to config the “MAILTO” parameter in the crontab file.