b1a67b881cee88388d64ff0e31cda81d24de96f7
2 # Copyright (C) 2016 Ian Kelling
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21 cbase
=$HOME/sysd-mail-once-state
26 Usage: sysd-mail-once [-t TO_ADDRESS] [-ERRORS] SERVICE COMMAND [ARGS...]
27 For systemd timers, email (with exim) on repeated failure & success after failure.
29 In the service triggered by the timer, prepend this script to the ExecStart.
30 The email will contain the service's logs for the last ERRORS runs.
33 Stores error counts in $cbase
35 -ERRORS: ERRORS is the number of failurs to accumulate before mailing the error.
52 c
=$cbase/$service # c for command file path base
55 arr
=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] ||
file=
56 [[ -d $cbase ]] || mkdir
-p $cbase
58 if [[ ! $file ]]; then
59 cursor
=$
(journalctl
--show-cursor -qn0|
sed 's/^\s*--\scursor:\s*//')
68 if (( i
< errors
)); then
72 if [[ $file == $c$errors ]]; then
78 printf "%s\n" "$cursor" >$file
79 if (( errors
== 1 )); then
86 From: $USER@$(hostname -f)
87 Subject: $HOSTNAME: $service exit code: $code
89 $(journalctl -u $service.service --after-cursor=$(<$file))
95 if [[ $file == $c$errors ]]; then
98 From: $USER@$(hostname -f)
99 Subject: $HOSTNAME: $service success