mail_error 519 B

1234567891011121314151617181920212223
  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
  5. function die { echo $1 ; exit 1; }
  6. PATH=$HOME/bin:$HOME/local/bin:$PATH
  7. while getopts "o:m:" c; do
  8. case $c in
  9. o) output_file=$OPTARG;;
  10. m) email=$OPTARG;;
  11. \*) echo "oops";;
  12. esac
  13. done
  14. shift $(($OPTIND-1))
  15. [[ -z $1 ]] && die "Must specify command"
  16. if ! "$@" > $output_file 2>&1 ; then
  17. mail -s "mail_error fail: $1" $email < $output_file
  18. fi