timer-mail: use journald instead of tempfile.
This commit is contained in:
parent
969ae70f64
commit
24ba29679f
1 changed files with 36 additions and 28 deletions
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
# vim: set noet sw=2 ts=2 sts=2:
|
||||||
|
|
||||||
help() {
|
help() {
|
||||||
[ 0 -lt $# ] && >&2 echo "$*"
|
[ 0 -lt $# ] && >&2 echo "$*"
|
||||||
|
@ -10,53 +11,60 @@ Options:
|
||||||
-t TO default: your login-user
|
-t TO default: your login-user
|
||||||
-f FROM default: your login-user
|
-f FROM default: your login-user
|
||||||
-s SUBJECT default: "timer: [command *args]"
|
-s SUBJECT default: "timer: [command *args]"
|
||||||
-v pipes output through
|
|
||||||
-e send email only on error (command exit-code != 0)
|
-e send email only on error (command exit-code != 0)
|
||||||
-o send email only if command writes on STDOUT or STDERR (default)
|
-o send email only if command writes on STDOUT or STDERR (default)
|
||||||
-a send always email
|
-a send always email
|
||||||
EOF
|
EOF
|
||||||
exit 1
|
exit 42
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if [ -z "$INVOCATION_ID" ]
|
||||||
|
then
|
||||||
|
>&2 echo 'No INVOCATION_ID set, not running systemd?'
|
||||||
|
fi
|
||||||
|
|
||||||
|
subject=
|
||||||
|
on=
|
||||||
|
to=
|
||||||
|
from=
|
||||||
|
|
||||||
eval set -- "$(getopt -n "$0" "ht:f:s:aev" "$@")"
|
eval set -- "$(getopt -n "$0" "ht:f:s:aev" "$@")"
|
||||||
while [ 0 -lt $# ]
|
while [ 0 -lt $# ]
|
||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h) help ;;
|
-h) help ;;
|
||||||
-s) shift ; subject="$1" ;;
|
-s) shift ; subject="$1" ;;
|
||||||
-t) shift ; to="$1" ;;
|
-t) shift ; to="$1" ;;
|
||||||
-f) shift ; from="$1" ;;
|
-f) shift ; from="$1" ;;
|
||||||
-e) on=error ;;
|
-e) on=error ;;
|
||||||
-o) on=output ;;
|
-o) on=output ;;
|
||||||
-a) on=always ;;
|
-a) on=always ;;
|
||||||
-v) verbose=true ;;
|
--) shift ; break ;;
|
||||||
--) shift ; break ;;
|
*) help "Unknown option: $1" ;;
|
||||||
*) help "Unknown option: $1" ;;
|
esac
|
||||||
esac
|
shift
|
||||||
shift
|
|
||||||
done
|
done
|
||||||
|
|
||||||
subject="${subject:-timer: $*}"
|
subject="${subject:-timer: $*}"
|
||||||
on=${on:-output}
|
on=${on:-output}
|
||||||
verbose=${verbose:-false}
|
|
||||||
to="${to:-${LOGNAME:-$USER}}"
|
to="${to:-${LOGNAME:-$USER}}"
|
||||||
from="${from:-${LOGNAME:-$USER}}"
|
from="${from:-${LOGNAME:-$USER}}"
|
||||||
|
|
||||||
output="$(mktemp)"
|
"$@"
|
||||||
trap "rm -f -- $output" EXIT
|
r=$?
|
||||||
|
|
||||||
if $verbose
|
# should a mail be sent?
|
||||||
then
|
mm=false
|
||||||
2>&1 "$@" | tee $output
|
case "$on" in
|
||||||
r=$?
|
always) mm=true ;;
|
||||||
else
|
error) [ 0 -lt $r ] && mm=true ;;
|
||||||
>$output 2>&1 "$@"
|
output) journalctl -n1 _SYSTEMD_INVOCATION_ID="$INVOCATION_ID" | grep -sqm1 "\S" && mm=true ;;
|
||||||
r=$?
|
esac
|
||||||
fi
|
|
||||||
|
|
||||||
if [ always = $on ] || [ error = $on -a 0 -lt $r ] || [ output = $on -a -s $output ]
|
if $mm
|
||||||
then
|
then
|
||||||
<$output mail -s "$subject" -r "$from" -- "$to" || exit 97
|
journalctl _SYSTEMD_INVOCATION_ID="$INVOCATION_ID" + INVOCATION_ID="$INVOCATION_ID" + USER_INVOCATION_ID="$INVOCATION_ID" | \
|
||||||
|
mail -s "$subject" -r "$from" -- "$to" || exit 97
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit $r
|
exit $r
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue