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.
20 local cbase c log x i out
file
21 cbase
=/var
/local
/cron-errors
22 [[ $EUID == 0 ]] || cbase
=$HOME/cron-errors
23 local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE]
25 For cronjobs, email log on repeated failure and success after failure.
27 Meant for use in cronjobs where LOG_MESSAGE or STDIN represents an error,
28 but we only want to output that to STDOUT if we've seen this type of
29 error ERRORS(default 3) number of times in a row, then we don't
30 want to output anything again until we've seen a success (an empty LOG_MESSAGE).
32 Logs LOG_MESSAGE or STDIN to /var/local/cron-errors/LOG_NAME\$error_count
33 or $HOME/cron-errors if not root, and keeps
34 state in the same directory.
36 -ERRORS: ERRORS is the number of errors to accumulate before outputing the error"
39 if [[ $1 == --help ]]; then
42 elif [[ $1 == -[0-9]* ]]; then
45 elif [[ $1 == -- ]]; then
53 # todo, make option & make them overridable based on command line or env variable
54 [[ -d $cbase ]] || mkdir
-p $cbase
56 # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script
60 # read stdin for anything which is not just a newline
61 elif [[ ! -t 0 ]]; then
68 file=($glob); [[ $file != "$glob" ]] ||
file=
73 if (( i
< errors
)); then
77 if [[ $file == $c$errors ]]; then
84 $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
88 $out $file <<<"${output[@]}"
95 if [[ $file == $c$errors ]]; then
96 echo "log-once success after failure for $c"