On Mar 14, Paul Wise <p...@debian.org> wrote:

> AFAICT OnSuccess/OnFailure services don't recieve the output of the
> succeeding/failing service. So the mail sending service needs to dig
> around in the systemd journal. Or make the service output go to a file,
> FIFO or socket and then send that to a mail.
Yes, this is true. These are the unit and script that I use, and I think 
that Debian would benefit from having something like this available in 
some common package.

I think that there is still space for cron jobs and for having a cron 
package installed by default, but also that packages should seriously 
consider migrating their cron jobs to timer units.

admin@rs1:~$ cat /etc/systemd/system/status-email@.service 
# Add this to the unit to be monitored:
#
# [Unit]
# OnFailure=status-email@%n.service

[Unit]
Description=Status mailer unit for %i

[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email n...@minap.it
Environment=FAILED_UNIT=%i
Environment=HOSTNAME=%H
admin@rs1:~$ cat /usr/local/bin/systemd-email 
#!/bin/sh -e

if [ "$1" ]; then
  MAILTO="$1"
else
  MAILTO="root"
fi

if [ -z "$FAILED_UNIT" ]; then
  echo "\$FAILED_UNIT is not set!" 2>&1
  exit 1
fi

fuu=$(systemctl show $FAILED_UNIT --property=User)
fuu="${fuu#User=}"
if [ "$fuu" ]; then
  shortfrom="$fuu@${HOSTNAME%%.*}"
else
  shortfrom="$root@${HOSTNAME%%.*}"
fi

exec /usr/sbin/sendmail -t <<EOF
To: $MAILTO
Subject: Systemd <$shortfrom> $FAILED_UNIT
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Auto-Submitted: auto-generated

$(systemctl status "$FAILED_UNIT" --full --lines=100)
EOF

-- 
ciao,
Marco

Attachment: signature.asc
Description: PGP signature

Reply via email to