Making cron send emails to the right address

Black and White photo of an old alarm clockIn my projects I use the cron program a lot. For those that aren’t familiar with cron, it is a system level program in a Unix/Linux environment that allows you to schedule programs to run a specific times. The list of commands to run, and when to run them, is stored in a crontab file.

Up until now I’d been using a .forward file to get the emails that cron sends to the right email address. The .forward file lives in the home directory of the user and makes the sendmail program forward email destined for that user to another address.

I discovered a much neater way of forwarding email now. There is a variable that you can set in your crontab file called mailto. Using this variable it is possible to specify an email address where the email is to be sent. For example to get email to be sent to example@example.net I would make the first line of my crontab file look like this.

MAILTO=example@example.net

Now any output from the commands that cron runs will be sent to the example@example.net address. It is a much neater solution.

Photo of the alarm clock found via the everystockphoto.com search engine. The photo was uploaded to flickr by user lukasd2009.

Posted in Musings. Tags: .

Leave a Reply