March 22, 2019

Automating Email Reminders to Yourself

Emailing With Myself

  • September 9, 2009
  • By Juliet Kemp

Juliet Kemp

I've occasionally found it useful to set up reminders to be emailed to myself on a regular basis. The most straightforward tool to use for this is cron with mail.

If you're sending directly from the command line, this command:

mail -s "Subject test" juliet@example.com

will not actually send the mail straight away. Instead, it will wait for you to type in the body of the email (which can be empty), and hit Ctrl-D twice. This may be a nuisance if you want an empty mail body and the message to be sent immediately. To mitigate that, you can try this:

mail -s "Subject test" juliet@example.com < /dev/null

If you're sending mail from cron, however, mail is running noninteractively, and it will behave slightly differently. On my (Debian stable) system, I don't need that redirect. Just using this line in the crontab:

59 8 * * 1-5 mail -s "Check backup ran" juliet@example.com

will send the mail at the appropriate time (0859 on Monday-Friday) with any empty body and without any intervention. However, this may vary between systems depending on exactly what program the mail command runs. On my system, this is /usr/bin/bsd-mail via a couple of soft links. If you do have problems, try adding the /dev/null input pipe as it appears on the command-line.

You may also want to redirect any output, which you can do by adding a redirect in the other direction:

59 8 * * 1-5 mail -s "Check backup ran" juliet@example.com > /dev/null

If you want to send an email that does have a body (e.g., the contents of a logfile), try:

15 4 * * * mail -s "Today's logs" juliet@example.com <

In fact, if you want a regular log email, LogWatch is a better choice. But you can use this to create any mail body: Just write and save an appropriate file.

Follow ServerWatch on Twitter

Article courtesy of ServerWatch

Most Popular LinuxPlanet Stories