X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=failmail;h=c420465829dafb1e1873ebc6445ca9a596a7a2be;hb=c95baa6003490dcbbda39f5b6340ab68ba121224;hp=703e13038d6c7428d32e9c4a3f21f6d84966317b;hpb=caf623ada289608bdbcc2216058057a7390f7939;p=log-quiet diff --git a/failmail b/failmail old mode 100644 new mode 100755 index 703e130..c420465 --- a/failmail +++ b/failmail @@ -14,19 +14,28 @@ # limitations under the License. case $1 in - -h|--help) - cat <<'EOF' -usage: logmail CMD [ARGS...] + -h|--help) + cat <<'EOF' +usage: failmail CMD [ARGS...] Send mail in case of failure of CMD + +Emails to $MAILTO or if unset, $USER@localhost EOF - exit 0 - ;; + exit 0 + ;; esac +mailto=$USER@localhost +if [[ $MAILTO ]]; then + mailto=$MAILTO +fi + t=$(mktemp) -if ! "$@" &>"$t"; then - mail -s "$HOSTNAME: $*" $USER@localhost <"$t" - rm "$t" +code=0 +"$@" &>"$t" || code=$? +if (( code )); then + mail -s "$HOSTNAME: code: $code cmd: $*" $mailto <"$t" + rm "$t" fi exit 0