return to PRS Technologies website


mail_any_output.sh
#!/usr/bin/ksh ############################################################################### # # Module: mail_any_output.sh # Author: Peter R. Schmidt # Description: Run any program - mail the stdout/stderr to a user # If no output - don't send the mail # # Note: This program is useful when you have a job that you want to # run in the cron, that sometimes sends output to stdout # (like an error message) and sometimes does not. # # This way, the person(s) only gets e-mailed when there is output. # # This gets around the default cron behavior of always e-mailing # the user whether there is a message or not, sending a blank # email to the user. # # Argument 1 Name of the program to run # Argument 2 Subject line of e-mail message # Argument 3 email address to receive the output (if any) # Argument 4 - n more email addresss to receive the output (optional) # # Note: You can run a program with runtime arguments or have a subject line # with multiple words if you surround them with quotes. i.e. # # mail_any_output.sh "my_program -a -b" "This is the subject" user1 user2 # # Change Log # # Date Name Description................. # 09/27/00 Peter R. Schmidt Start Program # ############################################################################### NUM_ARGS=$# if [ $NUM_ARGS -lt 3 ] then echo "Usage: mail_any_output.sh [program name] [subject line] [email 1] [email 2] [email n...]" exit 1 fi PROGRAM="$1" PGM_NO_ARGS=`echo $PROGRAM | cut -d" " -f1` # get program name without arguments SUBJECT="$2" ERR_FLAG=0 TMPDIR=${TMPDIR:-"/usr/tmp"} if [ ! -d $TMPDIR ] then TMPDIR=/tmp fi TMPFILE1=$TMPDIR/mail_any.$$.tmp rm -f $TMPFILE1 #------------------------------------------------------------------------------ type $PGM_NO_ARGS 2>&1 > /dev/null # Make sure program is executable if [ $? = 0 ] then $PROGRAM 2>&1 > $TMPFILE1 # Run the program - save stdout to a file else ERR_MSG="Error: mail_any_output.sh: program: ($PROGRAM) is not found." ERR_FLAG=1 fi # If output file is empty - exit now - do NOT send any e-mail !! if [ ! -s $TMPFILE1 -a $ERR_FLAG = 0 ] then rm -f $TMPFILE1 exit 0 fi #------------------------------------------------------------------------------ # e-mail output to all interested parties #------------------------------------------------------------------------------ shift shift while test $1 do if [ $ERR_FLAG = 1 ] then echo $ERR_MSG | mailx -s "$SUBJECT" $1 else cat $TMPFILE1 | mailx -s "$SUBJECT" $1 fi shift done rm -f $TMPFILE1 ################################################################################