2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
9 local cbase c log line i out
file o tmp
10 cbase
=/var
/local
/cron-errors
11 [[ $EUID == 0 ]] || cbase
=$HOME/cron-errors
12 local help="Usage: some-command-that-outputs-on-error |& log-once [OPTION]... LOG_NAME
14 Main use case: in cronjobs where STDIN represents an error,
15 but we only want to output that to STDOUT if we've seen this type of
16 error ERRORS(default 3) number of times in a row, then we don't
17 want to output anything again until we've seen a success: no standard input.
19 Logs STDIN to /var/local/cron-errors/LOG_NAME\$error_count or
20 $HOME/cron-errors if not root, and keeps state in the same directory.
22 -ERRORS: ERRORS is the number of errors to accumulate before outputing the error
25 You can emulate how cronjobs work by doing this for example:
27 cmdname |& log-once | ifne mail -s 'cmdname failed' root@localhost
29 I could imagine a similar command that considers its non-option args to
30 be an error, but I haven't written it yet.
34 if [[ $1 == --help ]]; then
37 elif [[ $1 == -[0-9]* ]]; then
40 elif [[ $1 == -- ]]; then
48 # todo, make option & make them overridable based on command line or env variable
49 [[ -d $cbase ]] || mkdir
-p $cbase
52 while read -r line
; do
54 # If we find something that is not just a newline:
61 # file is error file indicating previous error
62 tmp
=($glob); file="${tmp[0]}"
63 if [[ $file == "$glob" ]]; then
70 if (( i
< errors
)); then
74 if [[ $file == $c$errors ]]; then
80 if (( errors
== 1 )); then
84 $out $file <<<"log-once: $(date -R)"
88 for o in "${output[@]}"; do
97 if [[ $file == $c$errors ]]; then
98 echo "log-once: $(date -R): success after failure for $c"