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