fix stuck on email error
[log-quiet] / sysd-mail-once
index 4ccc81c3f37ba1a4ae2554818c2b7f7075de1895..b1a67b881cee88388d64ff0e31cda81d24de96f7 100755 (executable)
@@ -19,11 +19,12 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 errors=3
 cbase=$HOME/sysd-mail-once-state
 
 errors=3
 cbase=$HOME/sysd-mail-once-state
+to=root
 case "$1" in
   -h|--help)
     cat <<EOF
 case "$1" in
   -h|--help)
     cat <<EOF
-Usage: sysd-log-once [-ERRORS] SERVICE COMMAND [ARGS...]
-For systemd timers, email on repeated failure & success after failure.
+Usage: sysd-mail-once [-t TO_ADDRESS] [-ERRORS] SERVICE COMMAND [ARGS...]
+For systemd timers, email (with exim) on repeated failure & success after failure.
 
 In the service triggered by the timer, prepend this script to the ExecStart.
 The email will contain the service's logs for the last ERRORS runs.
 
 In the service triggered by the timer, prepend this script to the ExecStart.
 The email will contain the service's logs for the last ERRORS runs.
@@ -40,6 +41,10 @@ EOF
     errors=${1#-}
     shift
     ;;
     errors=${1#-}
     shift
     ;;
+  -t)
+    to="$2"
+    shift 2
+    ;;
 esac
 service=$1
 shift
 esac
 service=$1
 shift
@@ -76,14 +81,24 @@ if (( code )); then
     fi
   fi
   if $send_mail; then
     fi
   fi
   if $send_mail; then
-    journalctl -u $service.service --after-cursor=$(<$file) | \
-      mail -s "$HOSTNAME: $service exit code: $code" root
+      exim -odf -t <<EOF
+To: $to
+From: $USER@$(hostname -f)
+Subject: $HOSTNAME: $service exit code: $code
+
+$(journalctl -u $service.service --after-cursor=$(<$file))
+EOF
   fi
 else
   if [[ $file ]]; then
     rm -f $file
     if [[ $file == $c$errors ]]; then
   fi
 else
   if [[ $file ]]; then
     rm -f $file
     if [[ $file == $c$errors ]]; then
-      echo | mail -s "$HOSTNAME: $service success" root
+      exim -odf -t <<EOF
+To: $to
+From: $USER@$(hostname -f)
+Subject: $HOSTNAME: $service success
+
+EOF
     fi
   fi
 fi
     fi
   fi
 fi