Thursday, September 04, 2008

Howto: Sign Automated Emails [Unix]

As part of my work, I sometimes have to write scripts which email information out. For example, I have a script which periodically checks disk usage using the df command and emails an alert if greater than 90%. Other scripts query databases using isql or sqlplus and email reports to users. It is very useful to have these emails "signed" by the script, so that you can easily identify which script sent the email.

All of my scripts contain a sign function which simply prints out the hostname and the full name of the script being executed. Here is an example of it in action:

#!/bin/sh

#print out the host and script
sign()
{
    echo
    echo "-- "
    echo `hostname`:`pwd`/$0
}

#create email
SUBJECT=Test
TO=sharfah@xxx.com
echo "This is a test email" > $MAILFILE

#sign it
sign >> $MAILFILE

mailx -s "$SUBJECT" "$TO" < $MAILFILE

No comments:

Post a Comment