Skip to content

Commit

Permalink
Add '-l' switch to suppress output forwarding
Browse files Browse the repository at this point in the history
Add an option to disable command output being forwarded to checkrun's stdout.
This is useful for avoiding polluted journals when running systemd services.
  • Loading branch information
lynix committed Jun 2, 2022
1 parent 35cbd07 commit c7ca1ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ checkrun.sh supports the following command line options:
| -m | MAILTO | Set recipient (default: `$USER`) |
| -f | FORMAT | Set subject format string (use `%s` for command and `%d` for return value, default: `%s [%d]` |
| -q | | Do not sent output on exit code 0 |
| -l | | Do not forward output to `stdout` |
| -h | | Display usage information


Expand Down
13 changes: 9 additions & 4 deletions checkrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# checkrun.sh
# ---------------
# Cron-style mailing wrapper for systemd .timer units
# (C) 2018, 2019 Alexander Koch <mail@alexanderkoch.net>
# (C) Alexander Koch <mail@alexanderkoch.net>
#
# Released under the terms of the MIT License, see 'LICENSE'

Expand All @@ -19,6 +19,7 @@ MAILER="sendmail -t"
MAILTO="$USER"
SFORMAT="%s [%d]"
QUIET=0
NOLOG=0

# usage information
function print_usage() {
Expand All @@ -28,6 +29,7 @@ function print_usage() {
echo " -m MAILTO set recipient for notification mail (default: \$USER)"
echo " -f FORMAT set format string for mail subject (default: \"$SFORMAT\")"
echo " -q do not send command output on exit code 0"
echo " -l do not forward command output to stdout"
echo " -h display this usage information"
}

Expand All @@ -42,7 +44,7 @@ function mail() {


# parse cmdline options
while getopts ":s:m:f:qh" OPT; do
while getopts ":s:m:f:qlh" OPT; do
case "$OPT" in
s)
MAILER="$OPTARG"
Expand All @@ -56,6 +58,9 @@ while getopts ":s:m:f:qh" OPT; do
q)
QUIET=1
;;
l)
NOLOG=1
;;
h)
print_usage
exit 0
Expand Down Expand Up @@ -88,9 +93,9 @@ LOG="$(mktemp)"
$CMD &> "$LOG"
ERR=$?

# forward any output to controlling instance (e.g. journal)
# forward output to controlling instance (e.g. journal) if not disabled
LOG_LINES=$(wc -l "$LOG" | cut -d ' ' -f 1)
if [ $LOG_LINES -gt 0 ]; then
if [ $NOLOG -eq 0 ] && [ $LOG_LINES -gt 0 ]; then
cat "$LOG"
fi

Expand Down

0 comments on commit c7ca1ab

Please sign in to comment.