failcam minor: shellcheck warnings
[log-quiet] / sysd-mail-once
index e53d6ee6e5f41df4df6b78a702e5f3f3ae9e05e9..3b87eea08dbc7c4f399786d4ce5dc4ecaab84adc 100755 (executable)
@@ -19,11 +19,14 @@ 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 [COMMAND_ARGS...]
+
+For use with systemd timers, to 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.
@@ -33,6 +36,8 @@ Stores error counts in $cbase
 
 -ERRORS:  ERRORS is the number of failurs to accumulate before mailing the error.
           Default is 3.
 
 -ERRORS:  ERRORS is the number of failurs to accumulate before mailing the error.
           Default is 3.
+
+-t TO_ADDRESS:  Address to email about errors
 EOF
     exit 0
     ;;
 EOF
     exit 0
     ;;
@@ -40,45 +45,64 @@ EOF
     errors=${1#-}
     shift
     ;;
     errors=${1#-}
     shift
     ;;
+  -t)
+    to="$2"
+    shift 2
+    ;;
 esac
 service=$1
 shift
 
 c=$cbase/$service # c for command file path base
 
 esac
 service=$1
 shift
 
 c=$cbase/$service # c for command file path base
 
-glob="$c[0-9]*"
+glob="${c}[0-9]*"
 arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
 arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
-u=${USER:-root}
 [[ -d $cbase ]] || mkdir -p $cbase
 
 if [[ ! $file ]]; then
   cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
 fi
 [[ -d $cbase ]] || mkdir -p $cbase
 
 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
   if [[ $file ]]; then
-    i=${file#$c}
+    i=${file#"$c"}
     if (( i < errors )); then
       new_file=$c$((i+1))
       mv $file $new_file
       file=$new_file
       if [[ $file == $c$errors ]]; then
     if (( i < errors )); then
       new_file=$c$((i+1))
       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
       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
+      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
+      exim -odf -t <<EOF
+To: $to
+From: $USER@$(hostname -f)
+Subject: $HOSTNAME: $service success
+
+EOF
     fi
   fi
 fi
     fi
   fi
 fi