5

I'm trying to setup systemd to notify me via email if a unit fails, like Cron can be set up to do. I followed the guide on the Arch Linux wiki.

So I set-up a sendmail shell script in /usr/local/bin/systemd-email:

#!/bin/bash
/usr/bin/sendmail -t <<ERRMAIL
To: $1
From: systemd <root@$HOSTNAME>
Subject: $2
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8

$(systemctl status --full "$2")
ERRMAIL

And then I set-up a systemd service in /etc/systemd/system/status-email-user1@.service:

[Unit]
Description=status email for %I to user1

[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email user1@mailhost.com %i
User=nobody
Group=systemd-journal

However, when I test this setup, running systemctl start status-email-user1@dbus.service, I get the following fail message:

status-email-user1@dbus.service - status email for dbus to user1
   Loaded: loaded (/etc/systemd/system/status-email-user1@.service; static; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sun 2015-06-14 10:19:36 MDT; 20min ago
  Process: 31229 ExecStart=/usr/local/bin/systemd-email someone@gmail.com %i (code=exited, status=1/FAILURE)
 Main PID: 31229 (code=exited, status=1/FAILURE)

Jun 14 10:19:36 sindhu systemd[1]: Starting status email for dbus to user1...
Jun 14 10:19:36 sindhu sSMTP[31230]: /etc/ssmtp/ssmtp.conf not found
Jun 14 10:19:36 sindhu sSMTP[31230]: Unable to locate mailhub
Jun 14 10:19:36 sindhu systemd-email[31229]: sendmail: Cannot open mailhub:25
Jun 14 10:19:36 sindhu systemd[1]: status-email-user1@dbus.service: main pro...RE
Jun 14 10:19:36 sindhu systemd[1]: Failed to start status email for dbus to ...1.
Jun 14 10:19:36 sindhu systemd[1]: Unit status-email-user1@dbus.service ente...1.
Jun 14 10:19:36 sindhu systemd[1]: status-email-user1@dbus.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

But, /etc/ssmtp/ssmtp.conf is in fact present, and configured properly (I already have SSMTP set up), and so if I run something like:

echo -e "To: me@gmail.com\nFrom: someone@domain.com\nSubject: Hello Notification\n\n Hello, this is a test"|ssmtp me@gmail.com

I get email as expected/desired.

Likewise, if I run the shell script systemd-email someone@gmail.com dbus, I also get email.

So, all of the systemd-external pieces seem to be working; it therefore must be something within systemd, as far as I can make out. But I'm at a lost to know what, or how to fix it.

  • I have a very, very similar situation with systemd and postfix. I can find nothing in logs, the mail script runs when executed as a user, but systemd simply will not execute the mail command. I've tried everything I can think of and I'm about at my wit's end. – siliconrockstar Jun 3 '16 at 1:04
1

This thread fixed my similar issues. Basically, systemd is killing the child process that should send the mail before it ever gets to send the mail. Fix was to add 'set sendwait' to /etc/mail.rc, but my setup is a little different than what you've described.

/usr/bin/mail call in ExecStart not sending any mail

  • The -Ssendwait option to mail is (now) also mentioned on the Arch Wiki page. – Thomas Jan 9 '18 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.