# How to get cron job errors in email with MAILTO?

One of the neat features of [Cron](https://betterstack.com/community/guides/linux/cron-jobs-getting-started/) is the ability to send emails when an error
occurs during the execution of the cronjob. This can be done using the `MAILTO`
environmental variable. When executing cronjob, any output is mailed to the
owner of the crontab or to the user or email address specified in the `MAILTO`
environment variable in the crontab, if such exists.

[info]
## 🔭 Want to get alerted when your Cron doesn’t run correctly?
Go to [Better Stack](https://betterstack.com/uptime/) and start monitoring in 5 minutes.
[/info]

## Setting the MAILTO variable

`MAILTO` environmental variable is set in the same way as any other variable
using the `=` operator.

As a value of the variable, you can specify either a specific email address or a
specific user, or even an empty string in which case no email will be sent.

```bash
#mail to an email
MAILTO=email@example.com

#mail to a selected user
MAILTO=someuser
```

## Sending an email

First, open preferred crontab in the select text editor. Then assign a value to
the `MAILTO` variable on the line above the select cronjob as shown in the
example below.

```bash
MAILTO=email@example.com
*   *  *   *   *   /bin/backup.sh
```

This will execute the `/bin/backup.sh` shell script every minute. If the script
generates any output during the execution, it will be sent to
`email@example.com`.

Make sure that your script doesn't generate output if everything goes as planned
as it will send this output regardless of its nature.

[ad-uptime]