X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=failmail;h=c420465829dafb1e1873ebc6445ca9a596a7a2be;hb=af60b670d9960388b63ba6a1d8541b4cf312f5ca;hp=703e13038d6c7428d32e9c4a3f21f6d84966317b;hpb=d9bc271d0faf80847ed819cbd6f9220667ff43d8;p=log-quiet diff --git a/failmail b/failmail index 703e130..c420465 100755 --- 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