On 07.05.2023 16:07, G McAlister wrote:
On 07/05/2023 13:38, Per Jessen wrote:
Per Jessen wrote:
Yamaban wrote:
On Sun, 7 May 2023 11:55, Per Jessen <per@...> wrote:
I was just working on a new logrotate setup (and expecting to get some error output) when I realised systemd timers don't have any MAILTO option, like cron does.
Having googled it a bit, I see some concoctions involving OnFailure scripts, but is that currently the best/only solution? Thorsten Kukuk initated this sub-package in Nov.2022: https://manpages.opensuse.org/systemd-status-mail
That may be an answer (haven't tested it myself) Thanks, that looks useful. I guess I was thinking of an option for the timer unit, as a general thing, but I think this will do the job too. Hmm, slight twist - I want the output whenever there is any, i.e. same behaviour as with cron. I guess I have to use OnSuccess= and OnFailure= both. I'll check it out.
For me, OnSuccess and OnFailure didn't do what I needed ( an email notification regardless of success or failure but showing which).
I did this for notification of my nightly rmt job:
In the systemd rmt-server-mirror.service file, add an ExecStopPost:
ExecStopPost=/bin/bash "/usr/local/sbin/svcstop.sh"
Then create this script at /usr/local/sbin/svcstop.sh:
rmt:/etc/systemd/system # cat /usr/local/sbin/svcstop.sh #!/bin/bash # # Script to email on service stop # intended to be run by a systemd service
JRNL="$(/usr/bin/journalctl -u rmt-server-mirror.service -g 'WARN' --since yesterday)"
You should be able to collect messages for this exact invocation using journalctl -u rmt-server-mirror.service -g 'WARN' INVOCATION_ID=INVOCATION_ID
EMAIL="<my email address>" S1="Email from rmt, successful" S2="Email from rmt, failure"
if [[ $EXIT_STATUS == 0 ]]; then SUBJECT="$S1" else SUBJECT="$S2" fi
# the mailx option sendwait prevents it from forking, enabling systemd to manage it /usr/bin/mailx -Ssendwait -s "$SUBJECT" "$EMAIL" <<< "$JRNL"
exit 0