2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
9 local cbase c log x i out
file o tmp
10 cbase
=/var
/local
/cron-errors
11 [[ $EUID == 0 ]] || cbase
=$HOME/cron-errors
12 local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE...]
14 For cronjobs, email log on repeated failure and success after failure.
16 Meant for use in cronjobs where LOG_MESSAGE or STDIN represents an error,
17 but we only want to output that to STDOUT if we've seen this type of
18 error ERRORS(default 3) number of times in a row, then we don't
19 want to output anything again until we've seen a success (an empty LOG_MESSAGE).
21 Logs LOG_MESSAGE or STDIN to /var/local/cron-errors/LOG_NAME\$error_count
22 or $HOME/cron-errors if not root, and keeps
23 state in the same directory.
25 -ERRORS: ERRORS is the number of errors to accumulate before outputing the error"
28 if [[ $1 == --help ]]; then
31 elif [[ $1 == -[0-9]* ]]; then
34 elif [[ $1 == -- ]]; then
42 # todo, make option & make them overridable based on command line or env variable
43 [[ -d $cbase ]] || mkdir
-p $cbase
45 # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script
49 # read stdin for anything which is not just a newline
50 elif [[ ! -t 0 ]]; then
57 # file is error file indicating previous error
58 tmp
=($glob); file="${tmp[0]}"
59 if [[ $file == "$glob" ]]; then
66 if (( i
< errors
)); then
70 if [[ $file == $c$errors ]]; then
76 if (( errors
== 1 )); then
80 $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
84 for o in "${output[@]}"; do
93 if [[ $file == $c$errors ]]; then
94 echo "log-once success after failure for $c"