Daily testing of email deliveries using mailx

I needed a way to perform a daily check that a Linux email server is able to successfully transmit email to external email addresses. Such a test can easily be scheduled using cron and mailx. You can use the crontab utility to schedule mailx to run periodically and send a test message to a specified email address. E.g., the following entry will send a test message at five minutes after noon every day to john.doe@example.com:

05 12 * * * mailx -s "Daily email delivery test" john.doe@example.com </home/jan/Documents/daily_mail_test_message.txt

oreilly.com - Your tech ebook super store

The body of the message will contain the contents of the file /home/jan/Documents/daily_mail_test_message.txt.

The first 5 elements on the line in the crontab file are scheduling elements:

 # ┌───────────── min (0 - 59)
 # │ ┌────────────── hour (0 - 23)
 # │ │ ┌─────────────── day of month (1 - 31)
 # │ │ │ ┌──────────────── month (1 - 12)
 # │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
 # │ │ │ │ │                  Saturday, or use names; 7 is also Sunday)
 # │ │ │ │ │
 # │ │ │ │ │
 # * * * * *  command to execute

If an asterisk is used for an element, that indicates that scheduling is done for every possible value for that element. E.g., the 05 on the line indicates five minutes after the hour, in this case 12 (noon) and the following three asterisks indicate that the cron job should be run every day of every month and every day of the week, i.e., Sunday to Saturday. Use 24-hour clock time values, aka "military time", for the hours, e.g. 5:00 PM is 17.

To create the cron entry you can use crontab -e to edit the crontab file for your account - see the crontab man page for information on usage of the command. Crontab will normally use the vi text editor. If you are unfamiliar with the editor, if you type crontab -e, when you enter the editor, you can hit the G key to go to the end of the file, then hit the a key to append to the end of the file, then type or paste the appropriate line into the file. Then hit the Esc key to leave editing mode. Then hit the colon (:) key and type wq to write out the update file and quit the editor. If you make a mistake and don't want to save your changes, type q! after the colon. You can view the current contents of the crontab file with crontab -l.

For the man page for the mailx command, see mailx.

Related articles:

  1. Checking sendmail mail delivery from the command line
  2. Sending messages and files with mailx
  3. Location of cron files on a CentOS system

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px