Code

Sends email if non-zero return code from command
[nagiosplug.git] / tools / mail_error
1 #!/bin/bash
2 # mail_error -o file -m email_address command
3 # Runs command and redirects all output to file
4 # If command rc != 0, sends file to email_address
6 function die { echo $1 ; exit 1; }
8 while getopts "o:m:" c; do
9         case $c in
10                 o) output_file=$OPTARG;;
11                 m) email=$OPTARG;;
12                 \*) echo "oops";;
13         esac
14 done
15 shift $(($OPTIND-1))
16 echo "output_file=$output_file email=$email"
18 [[ -z $1 ]] && die "Must specify command"
20 if ! "$@" > $output_file 2>&1 ; then
21         mail -s "mail_error fail: $@" $email < $output_file
22 fi