;;
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
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
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