Automating email alerts on Unix systems

mailbox steve johnson
Credit: flickr / Steve Johnson

There are quite a few ways to send email from the Unix command line and so many good reasons for doing so. You might want to provide feedback on automated processes so that you and fellow admins know about problems long before they start having an effect. Or you might want to monitor critical processes or system resources. Whatever the motivation, alerts delivered via email provide a much more efficient method of managing systems than manually scanning through log files and checking on running processes. Taking advantage of some very easy-to-use scripting techniques, you might use automated email to report on disk space, send performance reports, tell you that a critical process had to be restarted, show who is logging in (and how frequently), report unusual events, and watch over a wide range of issues that need a sysadmin's attention.

A good sysadmin rule of thumb to consider: Make the problems announce themselves and you won't have to go looking for them.

The various tools that you can use for sending email from the command line include these, though only some of these commands lend themselves to sending email from a script.

  • mail and mailx
  • sendmail
  • telnet
  • mutt

The mail and mailx commands are pretty much the same. In fact, one is often simply a symbolic link to the other. Years ago, mailx might have been the best command when to use when automating email alerts and it was easy to include a subject line with your messages.

Here are some mailx examples, one with the entire message composed with an echo command and one that is sending a file. The first of these just sends a message. The second adds the output of a sar command (the output of which has been stored in a file) to the message.

$ echo "This is my message for you-oo-oo" | mailx -s "Don't worry"
$ mailx -s "`hostname` performance" < /tmp/sar-report

You can also use sendmail to send messages, but adding a subject line is a bit harder. Here we're sending the output of a sar command.

$ ( echo "Subject: `hostname` performance"; echo; cat sar-report ) | sendmail -v

And, if you don't want to see the sendmail commands going back and forth, add " > /dev/null" to the end of that to hide it from view.

$ ( echo "Subject: `hostname` performance"; echo; cat sar-report ) | sendmail -v > /dev/null

You can use telnet to communicate with a remote system and send it back by replicating the commands that mail servers require, but this process is generally interactive and a lot more work than the other commands. You have to enter commands like "HELO", "mail from:", and "rcpt to:". That great to see how servers chat with each other, but not a good technique to use with scripts.

Another command that you can use is mutt. Mutt is an email client, but can be used to send email as well. In this case, it works a lot like mailx. Either of these commands would work. Note that mutt in these examples is a drop-in for mailx.

$ mutt -s "`hostname` performance" < /tmp/sar-report
$ cat /tmp/sar-report |  mutt -s "`hostname` performance"

The script below uses mailx to send sar output via email. How it differs from the simple commands shown above is that it sends only the daily average from as many sar data files happen to be sitting in the /var/log/sa directory. It then labels each line in the output with the appropriate day of the week. Using a script like this, you could see the average performance of a system for the past week or more every day if this command were run from cron.


numDays=`ls /var/log/sa/sa?? | wc -l`
dayOfWk=`date +%u`

if [ $dayOfWk == 0 ]; then
    start=`expr $dayOfWk - 1`

echo "                    CPU     %user     %nice   %system   %iowait    %steal    %idle"

for file in `ls -tr /var/log/sa | grep -v sar`
    if [ $next == 7 ]; then
    case $next in
        0) day="Sunday...";;
        1) day="Monday...";;
        2) day="Tuesday..";;
        3) day="Wednesday";;
        4) day="Thursday.";;
        5) day="Friday...";;
        6) day="Saturday.";;
    sar -f /var/log/sa/$file | grep Average | sed "s/Average/$day/"
    next=`expr $next + 1`

cat /tmp/sar-report$$ | mailx -s "$sys Performance Summary" $recip
rm /tmp/sar-report$$

Once you get used to sending the results of automated checks via email, you can sit back and wait for the problems you didn't anticipate, knowing that the ones you're watching out for will announce themselves.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Must read: 10 new UI features coming to Windows 10