--- /dev/null
+#!/bin/bash
+# Copyright (C) 2016 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+
+errors=3
+cbase=/var/local/cron-errors
+case "$1" in
+ -h|--help)
+ cat <<EOF
+Usage: sysd-log-once [-ERRORS] SERVICE COMMAND
+
+Use to wrap systemd timers and to email on repeated failure & success after failure
+
+Stores error counts in $c\$error_count
+
+-ERRORS: ERRORS is the number of failurs to accumulate before mailing the error
+EOF
+ exit 0
+ ;;
+ -[0-9]*)
+ errors=${1#-}
+ shift
+ ;;
+esac
+service=$1
+shift
+
+c=$cbase/$service
+
+glob="$c[0-9]*"
+arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
+u=${USER:-root}
+[[ -d $cbase ]] || mkdir -p $cbase
+
+cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
+if "$@"; then
+ if [[ $file ]]; then
+ rm -f $file
+ if [[ $file == $c$errors ]]; then
+ echo | mail -s "$HOSTNAME: $service success" $u@localhost
+ fi
+ fi
+else
+ if [[ $file ]]; then
+ i=${file#$c}
+ 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=$cursor | \
+ mail -s "$HOSTNAME: $service failure" $u@localhost
+ fi
+ fi
+ else
+ file=${c}1
+ touch $file
+ fi
+fi