From 84cffb3bd9ae61b32f9922bfb5ab038834048a8e Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Sat, 30 Oct 2021 15:52:14 -0400 Subject: [PATCH] save exit codes --- failmail | 6 ++++-- logmail | 6 ++++-- sysd-mail-once | 30 ++++++++++++++++++------------ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/failmail b/failmail index 6360c7e..c420465 100755 --- 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 794615b..bece0ae 100755 --- 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 diff --git a/sysd-mail-once b/sysd-mail-once index e53d6ee..711479c 100755 --- a/sysd-mail-once +++ b/sysd-mail-once @@ -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 -- 2.30.2