2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
26 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
30 cbase
="${tmp[0]}/sysd-mail-once-state"
34 while [[ $1 == -* ]]; do
38 Usage: sysd-mail-once [-t TO_ADDRESS] [-ERRORS] SERVICE COMMAND [COMMAND_ARGS...]
40 For use with systemd timers, to email (with exim) on repeated failure &
41 success after failure.
43 In the service triggered by the timer, prepend this script to the ExecStart.
44 The email will contain the service's logs for the last ERRORS runs.
47 Stores error counts in $cbase
49 -ERRORS ERRORS is the number of failurs to accumulate before mailing the error.
52 -a Email the logs of all errors intead of just the last one.
53 -t TO_ADDRESS Address to email about errors
54 -n Dry run. Execute command but only print out what we would do in response.
72 echo "error: unexpected arg: $1"
80 echo "error: expected at least 2 args after options. args:"
88 # maybe run, depending on $dryrun
96 # maybe run, with stdin
99 printf "%s <<'EOF'\n" "$*"
108 printf "dryrun: %s\n" "$*"
112 c
=$cbase/$service # c for command file path base
117 arr
=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] ||
file=
122 if ! [[ -d $cbase ]]; then
131 cursor
=$
(journalctl
--show-cursor -qn0|
sed 's/^\s*--\scursor:\s*//')
134 if (( i
< errors
)); then
138 if [[ $file == $c$errors ]]; then
142 printf "dryrun: appending to file $file: %s\n" "$cursor"
144 printf "%s\n" "$cursor" >>$file
151 printf "dryrun: creating $file, contents: %s\n" "$cursor"
153 printf "%s\n" "$cursor" >$file
155 if (( errors
== 1 )); then
161 cursor
=$
(head -n1 $file)
163 cursor
=$
(tail -n1 $file)
165 echo "sysd-mail-once: emailing on $errors errors. exit code: $code"
166 mi exim
-odf -t <<EOF
168 From: $(id -u -n)@$(hostname -f)
169 Subject: $HOSTNAME: $service exit code: $code
171 $(journalctl -u $service.service --after-cursor="$cursor")
177 if [[ $file == $c$errors ]]; then
178 echo "sysd-mail-once: emailing success after >= $errors errors."
179 mi exim
-odf -t <<EOF
181 From: $(id -u -n)@$(hostname -f)
182 Subject: $HOSTNAME: $service success