save exit codes
authorIan Kelling <iank@fsf.org>
Sat, 30 Oct 2021 19:52:14 +0000 (15:52 -0400)
committerIan Kelling <iank@fsf.org>
Sat, 30 Oct 2021 19:52:14 +0000 (15:52 -0400)
failmail
logmail
sysd-mail-once

index 6360c7e31c0d84d7d3e2262adc925154683417a1..c420465829dafb1e1873ebc6445ca9a596a7a2be 100755 (executable)
--- a/failmail
+++ b/failmail
@@ -32,8 +32,10 @@ if [[ $MAILTO ]]; then
 fi
 
 t=$(mktemp)
-if ! "$@" &>"$t"; then
-  mail -s "$HOSTNAME: $*" $mailto <"$t"
+code=0
+"$@" &>"$t" || code=$?
+if (( code )); then
+  mail -s "$HOSTNAME: code: $code cmd: $*" $mailto <"$t"
   rm "$t"
 fi
 exit 0
diff --git a/logmail b/logmail
index 794615b6c9d0174d25195a5083d303be095ff148..bece0ae8683ecd72818599f9a42dcb5d6e312200 100755 (executable)
--- a/logmail
+++ b/logmail
@@ -26,8 +26,10 @@ EOF
     ;;
 esac
 t=$(mktemp)
-if ! "$@" &>"$t" || [[ -s $t ]]; then
-  mail -s "$HOSTNAME: $*" $mailto <"$t"
+code=0
+"$@" &>"$t" || code=$?
+if (( code )) || [[ -s $t ]]; then
+  mail -s "$HOSTNAME: code: $code cmd: $*" $mailto <"$t"
   rm "$t"
 fi
 exit 0
index e53d6ee6e5f41df4df6b78a702e5f3f3ae9e05e9..711479c84b1dbecdeefbfa76b64122eaeb09467a 100755 (executable)
@@ -54,14 +54,11 @@ u=${USER:-root}
 if [[ ! $file ]]; then
   cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
 fi
-if "$@"; then
-  if [[ $file ]]; then
-    rm -f $file
-    if [[ $file == $c$errors ]]; then
-      echo | mail -s "$HOSTNAME: $service success" $u@localhost
-    fi
-  fi
-else # $@ failed
+
+code=0
+"$@" || code=$?
+if (( code )); then
+  send_mail=false
   if [[ $file ]]; then
     i=${file#$c}
     if (( i < errors )); then
@@ -69,16 +66,25 @@ else # $@ failed
       mv $file $new_file
       file=$new_file
       if [[ $file == $c$errors ]]; then
-        journalctl -u $service.service --after-cursor=$(<$file) | \
-          mail -s "$HOSTNAME: $service failure" $u@localhost
+        send_mail=true
       fi
     fi
   else
     file=${c}1
     printf "%s\n" "$cursor" >$file
     if (( errors == 1 )); then
-      journalctl -u $service.service --after-cursor=$(<$file) | \
-        mail -s "$HOSTNAME: $service failure" $u@localhost
+      send_mail=true
+    fi
+  fi
+  if $send_mail; then
+    journalctl -u $service.service --after-cursor=$(<$file) | \
+      mail -s "$HOSTNAME: $service exit code: $code" $u@localhost
+  fi
+else
+  if [[ $file ]]; then
+    rm -f $file
+    if [[ $file == $c$errors ]]; then
+      echo | mail -s "$HOSTNAME: $service success" $u@localhost
     fi
   fi
 fi