7

I have a critical application which is run as a service by systemd.

It is set up to restart as soon as there is a failure.

How to send an email if the application restarts?

  • 2
    I don't know how to do it with systemd, but with monit you can have it watch a process, and it can send out a notification if the process id changes. – Zoredache Sep 30 '17 at 2:55
12

First you need two files: an executable for sending the mail and a .service for starting the executable. For this example, the executable is just a shell script using sendmail:

/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

Whatever executable you use, it should probably take at least two arguments as this shell script does: the address to send to and the unit file to get the status of. The .service we create will pass these arguments:

/etc/systemd/system/status-email-user@.service:

[Unit]
Description=status email for %i to user

[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email address %i
User=nobody
Group=systemd-journal

Where user is the user being emailed and address is that user's email address. Although the recipient is hard-coded, the unit file to report on is passed as an instance parameter, so this one service can send email for many other units. At this point you can start status-email-user@dbus.service to verify that you can receive the emails.

Then simply edit the service you want emails for and add OnFailure=status-email-user@%n.service to the [Unit] section. %n passes the unit's name to the template.

Source: archlinux wiki: systemd timers MAILTO

  • But @Dave doesn't require another service. He want the service he already uses to be able to send mail on each start/restart. For that, there is ExecStartPost option. – Jaroslav Kucera Sep 30 '17 at 12:57
  • @JaroslavKucera I think this to decide it up to the OP... :) Also, I'm not sure if ExecStartPost is the right choice: It would also trigger after a "normal" start, not only in case of a failure, right? – gf_ Sep 30 '17 at 13:14
  • I'm interested why this get's down voted - please raise your voices and speak up! – gf_ Sep 30 '17 at 13:15
  • Because the question isn't about another service, but modification of existing service. Yes, ExecStartPost would trigger mail send even on normal start. I'm not aware of anything which would work only on restart. – Jaroslav Kucera Sep 30 '17 at 13:17
  • @JaroslavKucera Well, it seems the OP disagrees, but of course, keep your opinion. – gf_ Sep 30 '17 at 13:18
0

You may try to use systemd service option ExecStartPost.

The description is available here:

https://www.freedesktop.org/software/systemd/man/systemd.service.html

There can be more declarations of this option in the service definition file. It's triggered one by one.

You'll have some examples in your system as well.

-1

You can create a shell script to check service status and send email while server booting up. This link might help you

https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up

  • 1
    This question is not about booting, but about a service restarting, which might happen quite some time after booting. Hence, I'm unsure if your answer is of any help. – gf_ Sep 30 '17 at 7:29

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.