dont email usually redundant logs by default
[log-quiet] / failmail
index 703e13038d6c7428d32e9c4a3f21f6d84966317b..c420465829dafb1e1873ebc6445ca9a596a7a2be 100755 (executable)
--- a/failmail
+++ b/failmail
 # 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