save exit codes
[log-quiet] / sysd-mail-once
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