2021-04-10 22:11:19 +02:00
|
|
|
#!/usr/bin/env sh
|
2024-10-23 17:07:03 +02:00
|
|
|
# vim: set noet sw=2 ts=2 sts=2:
|
2021-04-10 22:11:19 +02:00
|
|
|
|
|
|
|
help() {
|
|
|
|
[ 0 -lt $# ] && >&2 echo "$*"
|
|
|
|
>&2 cat <<EOF
|
|
|
|
Usage: $0 [*options] command [*args]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h
|
|
|
|
-t TO default: your login-user
|
|
|
|
-f FROM default: your login-user
|
|
|
|
-s SUBJECT default: "timer: [command *args]"
|
|
|
|
-e send email only on error (command exit-code != 0)
|
2024-11-17 09:19:28 +01:00
|
|
|
-o send email only if command writes something to syslog (default)
|
|
|
|
In Services, stdout and stderr are logged to syslog.
|
2021-04-10 22:11:19 +02:00
|
|
|
-a send always email
|
|
|
|
EOF
|
2024-10-23 17:07:03 +02:00
|
|
|
exit 42
|
2021-04-10 22:11:19 +02:00
|
|
|
}
|
|
|
|
|
2024-10-23 17:07:03 +02:00
|
|
|
if [ -z "$INVOCATION_ID" ]
|
|
|
|
then
|
|
|
|
>&2 echo 'No INVOCATION_ID set, not running systemd?'
|
|
|
|
fi
|
|
|
|
|
|
|
|
subject=
|
|
|
|
on=
|
|
|
|
to=
|
|
|
|
from=
|
|
|
|
|
2021-04-10 22:11:19 +02:00
|
|
|
eval set -- "$(getopt -n "$0" "ht:f:s:aev" "$@")"
|
|
|
|
while [ 0 -lt $# ]
|
|
|
|
do
|
2024-10-23 17:07:03 +02:00
|
|
|
case "$1" in
|
|
|
|
-h) help ;;
|
|
|
|
-s) shift ; subject="$1" ;;
|
|
|
|
-t) shift ; to="$1" ;;
|
|
|
|
-f) shift ; from="$1" ;;
|
|
|
|
-e) on=error ;;
|
|
|
|
-o) on=output ;;
|
|
|
|
-a) on=always ;;
|
|
|
|
--) shift ; break ;;
|
|
|
|
*) help "Unknown option: $1" ;;
|
|
|
|
esac
|
|
|
|
shift
|
2021-04-10 22:11:19 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
subject="${subject:-timer: $*}"
|
|
|
|
on=${on:-output}
|
|
|
|
to="${to:-${LOGNAME:-$USER}}"
|
|
|
|
from="${from:-${LOGNAME:-$USER}}"
|
|
|
|
|
2024-10-23 17:07:03 +02:00
|
|
|
"$@"
|
|
|
|
r=$?
|
2021-04-10 22:11:19 +02:00
|
|
|
|
2024-10-23 17:07:03 +02:00
|
|
|
# should a mail be sent?
|
|
|
|
mm=false
|
|
|
|
case "$on" in
|
|
|
|
always) mm=true ;;
|
|
|
|
error) [ 0 -lt $r ] && mm=true ;;
|
|
|
|
output) journalctl -n1 _SYSTEMD_INVOCATION_ID="$INVOCATION_ID" | grep -sqm1 "\S" && mm=true ;;
|
|
|
|
esac
|
2021-04-10 22:11:19 +02:00
|
|
|
|
2024-10-23 17:07:03 +02:00
|
|
|
if $mm
|
2021-04-10 22:11:19 +02:00
|
|
|
then
|
2024-10-23 17:07:03 +02:00
|
|
|
journalctl _SYSTEMD_INVOCATION_ID="$INVOCATION_ID" + INVOCATION_ID="$INVOCATION_ID" + USER_INVOCATION_ID="$INVOCATION_ID" | \
|
|
|
|
mail -s "$subject" -r "$from" -- "$to" || exit 97
|
2021-04-10 22:11:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit $r
|