# 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
# 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.
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 "$@"
# 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
# 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 "$@"
# 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
}
[[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
usage() {
- cat <<EOF
+ cat <<EOF
Usage: ${0##*/} []
Install or uninstall files to /usr/local/bin
Note: Uses GNU getopt options parsing style
EOF
- exit $1
+ exit $1
}
dry=false
uninstall=false # default
temp=$(getopt -l help,uninstall,dry-run hun "$@") || usage 1
eval set -- "$temp"
while true; do
- case $1 in
- -n|--dry-run) dry=true; shift ;;
- -u|--uninstall) uninstall=true; shift ;;
- -h|--help) usage ;;
- --) shift; break ;;
- *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
- esac
+ case $1 in
+ -n|--dry-run) dry=true; shift ;;
+ -u|--uninstall) uninstall=true; shift ;;
+ -h|--help) usage ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
+ esac
done
files=()
for f in *; do
- if [[ -x $f && ! -d $f && ! -L $f && $f != setup ]]; then
- files+=($f)
- fi
+ if [[ -x $f && ! -d $f && ! -L $f && $f != setup ]]; then
+ files+=($f)
+ fi
done
if $uninstall; then
- if $dry; then
- echo "setup dry run: cd /usr/local/bin"
- echo "setup dry run: rm -fv ${files[*]}"
- else
- cd /usr/local/bin
- rm -fv ${files[@]}
- fi
+ if $dry; then
+ echo "setup dry run: cd /usr/local/bin"
+ echo "setup dry run: rm -fv ${files[*]}"
+ else
+ cd /usr/local/bin
+ rm -fv ${files[@]}
+ fi
else
- if $dry; then
- echo "setup dry run: install -v ${files[*]} /usr/local/bin"
- else
- install -v ${files[@]} /usr/local/bin
- fi
+ if $dry; then
+ echo "setup dry run: install -v ${files[*]} /usr/local/bin"
+ else
+ install -v ${files[@]} /usr/local/bin
+ fi
fi
errors=3
cbase=$HOME/sysd-mail-once-state
case "$1" in
- -h|--help)
- cat <<EOF
+ -h|--help)
+ cat <<EOF
Usage: sysd-log-once [-ERRORS] SERVICE COMMAND [ARGS...]
For systemd timers, email on repeated failure & success after failure.
-ERRORS: ERRORS is the number of failurs to accumulate before mailing the error.
Default is 3.
EOF
- exit 0
- ;;
- -[0-9]*)
- errors=${1#-}
- shift
- ;;
+ exit 0
+ ;;
+ -[0-9]*)
+ errors=${1#-}
+ shift
+ ;;
esac
service=$1
shift
[[ -d $cbase ]] || mkdir -p $cbase
if [[ ! $file ]]; then
- cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
+ cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
fi
if "$@"; then
- if [[ $file ]]; then
- rm -f $file
- if [[ $file == $c$errors ]]; then
- echo | mail -s "$HOSTNAME: $service success" $u@localhost
- fi
+ if [[ $file ]]; then
+ rm -f $file
+ if [[ $file == $c$errors ]]; then
+ echo | mail -s "$HOSTNAME: $service success" $u@localhost
fi
+ fi
else # $@ failed
- 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
+ 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