From: Ian Kelling Date: Mon, 5 Aug 2019 03:23:28 +0000 (-0400) Subject: change indent level to 2 X-Git-Url: https://iankelling.org/git/?p=log-quiet;a=commitdiff_plain;h=ea9d543d812b5ec13d4d3c6b424626f3afaaa5dc change indent level to 2 --- diff --git a/failmail b/failmail index 703e130..377564e 100755 --- a/failmail +++ b/failmail @@ -14,19 +14,19 @@ # limitations under the License. case $1 in - -h|--help) - cat <<'EOF' + -h|--help) + cat <<'EOF' usage: logmail CMD [ARGS...] Send mail in case of failure of CMD EOF - exit 0 - ;; + exit 0 + ;; esac t=$(mktemp) if ! "$@" &>"$t"; then - mail -s "$HOSTNAME: $*" $USER@localhost <"$t" - rm "$t" + mail -s "$HOSTNAME: $*" $USER@localhost <"$t" + rm "$t" fi exit 0 diff --git a/log-once b/log-once index 179dacf..5980b81 100755 --- a/log-once +++ b/log-once @@ -14,13 +14,13 @@ # limitations under the License. append() { - cat >> "$1" + cat >> "$1" } log-once() { - local cbase c log x i out file - cbase=/var/local/cron-errors - [[ $EUID == 0 ]] || cbase=$HOME/cron-errors - local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] + local cbase c log x i out file + cbase=/var/local/cron-errors + [[ $EUID == 0 ]] || cbase=$HOME/cron-errors + local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] For cronjobs, email log on repeated failure and success after failure. @@ -34,68 +34,68 @@ or $HOME/cron-errors if not root, and keeps state in the same directory. -ERRORS: ERRORS is the number of errors to accumulate before outputing the error" - errors=3 - while true; do - if [[ $1 == --help ]]; then - echo "$help" - return - elif [[ $1 == -[0-9]* ]]; then - errors=${1#-} - shift - elif [[ $1 == -- ]]; then - shift - break - else - break - fi - done - log_name=$1 - # todo, make option & make them overridable based on command line or env variable - [[ -d $cbase ]] || mkdir -p $cbase - c=$cbase/$log_name - # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script - log=false - if [[ $2 ]]; then - log=true - # read stdin for anything which is not just a newline - elif [[ ! -t 0 ]]; then - while read -r x; do - output+=( $x ) - [[ $x ]] && log=true - done - fi - glob="$c[0-9]*" - file=($glob); [[ $file != "$glob" ]] || file= - if $log; then - out=append - if [[ $file ]]; then - i="${file#$c}" - if (( i < errors )); then - new_file=$c$((i+1)) - mv $file $new_file - file=$new_file - if [[ $file == $c$errors ]]; then - out="tee -a" - fi - fi - else - file=${c}1 - fi - $out $file <<<"log-once: $(date "+%A, %B %d, %r")" - if [[ $2 ]]; then - $out $file <<<"$2" - else - $out $file <<<"${output[@]}" - $out $file - fi - return 0 + errors=3 + while true; do + if [[ $1 == --help ]]; then + echo "$help" + return + elif [[ $1 == -[0-9]* ]]; then + errors=${1#-} + shift + elif [[ $1 == -- ]]; then + shift + break + else + break fi + done + log_name=$1 + # todo, make option & make them overridable based on command line or env variable + [[ -d $cbase ]] || mkdir -p $cbase + c=$cbase/$log_name + # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script + log=false + if [[ $2 ]]; then + log=true + # read stdin for anything which is not just a newline + elif [[ ! -t 0 ]]; then + while read -r x; do + output+=( $x ) + [[ $x ]] && log=true + done + fi + glob="$c[0-9]*" + file=($glob); [[ $file != "$glob" ]] || file= + if $log; then + out=append if [[ $file ]]; then - rm -f $file + i="${file#$c}" + if (( i < errors )); then + new_file=$c$((i+1)) + mv $file $new_file + file=$new_file if [[ $file == $c$errors ]]; then - echo "log-once success after failure for $c" + out="tee -a" fi + fi + else + file=${c}1 + fi + $out $file <<<"log-once: $(date "+%A, %B %d, %r")" + if [[ $2 ]]; then + $out $file <<<"$2" + else + $out $file <<<"${output[@]}" + $out $file fi return 0 + fi + if [[ $file ]]; then + rm -f $file + if [[ $file == $c$errors ]]; then + echo "log-once success after failure for $c" + fi + fi + return 0 } log-once "$@" diff --git a/logmail b/logmail index cac1e21..4aad395 100755 --- a/logmail +++ b/logmail @@ -14,18 +14,18 @@ # limitations under the License. case $1 in - -h|--help) - cat <<'EOF' + -h|--help) + cat <<'EOF' usage: logmail CMD [ARGS...] Send mail in case of output or failure of CMD EOF - exit 0 - ;; + exit 0 + ;; esac t=$(mktemp) if ! "$@" &>"$t" || [[ ! -s $t ]]; then - mail -s "$HOSTNAME: $*" $USER@localhost <"$t" - rm "$t" + mail -s "$HOSTNAME: $*" $USER@localhost <"$t" + rm "$t" fi exit 0 diff --git a/logq b/logq index b253bef..08b059d 100755 --- a/logq +++ b/logq @@ -16,58 +16,58 @@ # This file is exactly the same as logq-function except the last line # to make it into a script. logq () { - local help="Usage: logq [-h|--help] COMMAND [ARG...] + local help="Usage: logq [-h|--help] COMMAND [ARG...] Log to temp file, output tail of log in case of error. Run COMMAND with ARGs. Output return code, command, and log path. Tail log if there is a failure. Follows output format of logsave." - if [[ $1 == --help || $1 == -h ]]; then - echo "$help" - return - fi - if [[ $# == 0 ]]; then - echo "error: need 1 or more arguments + if [[ $1 == --help || $1 == -h ]]; then + echo "$help" + return + fi + if [[ $# == 0 ]]; then + echo "error: need 1 or more arguments $help" - return 1 - fi + return 1 + fi - # deliniate arguments, so spaces aren't ambiguous - local index=0 - local x prettycommand - for x in "$@"; do - prettycommand+="[$index]$x " - index=$(( index+1 )) - done + # deliniate arguments, so spaces aren't ambiguous + local index=0 + local x prettycommand + for x in "$@"; do + prettycommand+="[$index]$x " + index=$(( index+1 )) + done - local file="$*" - file="$(mktemp -d)/${file//[[:space:]\/]/_}" - # give us ~20 char filename max - file="${file:0:40}" + local file="$*" + file="$(mktemp -d)/${file//[[:space:]\/]/_}" + # give us ~20 char filename max + file="${file:0:40}" - printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" + printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" - if [[ $- != *x* ]]; then - echo "log $file = $@" - fi + if [[ $- != *x* ]]; then + echo "log $file = $@" + fi - # we will propagate any errors - local logq_ret=$( - set +e - trap ERR - "$@" &>> "$file" - echo $? - ) - printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file" + # we will propagate any errors + local logq_ret=$( + set +e + trap ERR + "$@" &>> "$file" + echo $? + ) + printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file" - if [[ $logq_ret != 0 ]]; then - x="tail -n 100 $file" - if [[ $- != *x* ]]; then - echo "logq failure. $x :" - fi - $x + if [[ $logq_ret != 0 ]]; then + x="tail -n 100 $file" + if [[ $- != *x* ]]; then + echo "logq failure. $x :" fi - return $logq_ret + $x + fi + return $logq_ret } logq "$@" diff --git a/logq-function b/logq-function index d07cba0..2ae5dae 100644 --- a/logq-function +++ b/logq-function @@ -13,56 +13,56 @@ # See the License for the specific language governing permissions and # limitations under the License. logq () { - local help="Usage: logq [-h|--help] COMMAND [ARG...] + local help="Usage: logq [-h|--help] COMMAND [ARG...] Log Quietly. Run COMMAND with ARGs, log to temp file. Output return code, command, and log path. Tail log if there is a failure. Follows output format of logsave." - if [[ $1 == --help || $1 == -h ]]; then - echo "$help" - return - fi - if [[ $# == 0 ]]; then - echo "error: need 1 or more arguments + if [[ $1 == --help || $1 == -h ]]; then + echo "$help" + return + fi + if [[ $# == 0 ]]; then + echo "error: need 1 or more arguments $help" - return 1 - fi + return 1 + fi - # deliniate arguments, so spaces aren't ambiguous - local index=0 - local x prettycommand - for x in "$@"; do - prettycommand+="[$index]$x " - index=$(( index+1 )) - done + # deliniate arguments, so spaces aren't ambiguous + local index=0 + local x prettycommand + for x in "$@"; do + prettycommand+="[$index]$x " + index=$(( index+1 )) + done - local file="$*" - file="$(mktemp -d)/${file//[[:space:]\/]/_}" - # give us ~20 char filename max - file="${file:0:40}" + local file="$*" + file="$(mktemp -d)/${file//[[:space:]\/]/_}" + # give us ~20 char filename max + file="${file:0:40}" - printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" + printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" - if [[ $- != *x* ]]; then - echo "log $file = $@" - fi + if [[ $- != *x* ]]; then + echo "log $file = $@" + fi - # we will propagate any errors - local logq_ret=$( - set +e - trap ERR - "$@" &>> "$file" - echo $? - ) - printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file" + # we will propagate any errors + local logq_ret=$( + set +e + trap ERR + "$@" &>> "$file" + echo $? + ) + printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file" - if [[ $logq_ret != 0 ]]; then - x="tail -n 100 $file" - if [[ $- != *x* ]]; then - echo "logq failure. $x :" - fi - $x + if [[ $logq_ret != 0 ]]; then + x="tail -n 100 $file" + if [[ $- != *x* ]]; then + echo "logq failure. $x :" fi - return $logq_ret + $x + fi + return $logq_ret } diff --git a/setup b/setup index 44ef3f5..7ce3624 100755 --- a/setup +++ b/setup @@ -19,7 +19,7 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@" usage() { - cat <&2' ERR errors=3 cbase=$HOME/sysd-mail-once-state case "$1" in - -h|--help) - cat <$file + if [[ $file ]]; then + i=${file#$c} + if (( i < errors )); then + new_file=$c$((i+1)) + mv $file $new_file + file=$new_file + if [[ $file == $c$errors ]]; then + journalctl -u $service.service --after-cursor=$(<$file) | \ + mail -s "$HOSTNAME: $service failure" $u@localhost + fi fi + else + file=${c}1 + printf "%s\n" "$cursor" >$file + fi fi