Skip to content

Instantly share code, notes, and snippets.

@meramsey
Forked from benediktg/readme.md
Last active January 22, 2022 22:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meramsey/ae29ae89e8bbcb4dd28f834db8608eb9 to your computer and use it in GitHub Desktop.
Save meramsey/ae29ae89e8bbcb4dd28f834db8608eb9 to your computer and use it in GitHub Desktop.
Send mails from systemd services

Usage

based on https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO

  • Move systemd-email.sh to /usr/local/bin/systemd-email
  • Move status-email-alerts@.service to /etc/systemd/system/
  • Put OnFailure=OnFailure=status-email-alerts@%n.service into the [Unit] section of a service

Create file /usr/local/bin/systemd-email

touch /usr/local/bin/systemd-email && chmod +x /usr/local/bin/systemd-email

Or via wget:

wget -O /usr/local/bin/systemd-email https://gist.githubusercontent.com/whattheserver/ae29ae89e8bbcb4dd28f834db8608eb9/raw/303445769bf9a5b8eeb05adedfd0b87229b740ca/systemd-email.sh && chmod +x /usr/local/bin/systemd-email; 

Edit if desired:

$EDITOR /usr/local/bin/systemd-email

Create file

touch /etc/systemd/system/status-email-alerts@.service

Edit:

via editor

$EDITOR /etc/systemd/system/status-email-alerts@.service

via systemctl (Recommended)

systemctl edit --full status-email-alerts@.service

Or download and immediately edit it vis systemctl to change the email.

wget -O /etc/systemd/system/status-email-alerts@.service https://gist.githubusercontent.com/whattheserver/ae29ae89e8bbcb4dd28f834db8608eb9/raw/55a4946b643e3d4fd93d1270a480c6dc66f360bb/status-email-alerts@.service && systemctl edit --full status-email-alerts@.service

To test Set your sleep to lower number in /usr/local/bin/systemd-email and execute with your desired email and service name:

/usr/local/bin/systemd-email youremail@example.com YOUR_SERVICE_HERE.service
[Unit]
Description=Status email for %i to user
[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email mail@example.org %i
User=nobody
Group=systemd-journal
#!/usr/bin/env bash
set -eu
# Format service name so its shorter in subject
SERVICE=${2//.service/}
# Set your sleep interval slightly longer then systemd restartinterval on the applicable services RestartSec=60s
# https://www.gnu.org/software/coreutils/manual/html_node/sleep-invocation.html
SLEEP_INTERVAL='65s'
NOTIFICATION_STATUS=$(systemctl status --full "$2")
# Sleeping so we can allow service to try to recover first and then send alert after having allowed it to restart.
sleep "${SLEEP_INTERVAL}"
FOLLOWUP_STATUS=$(systemctl status --full "$2")
# sendmail is in /usr/sbin on openSUSE
/usr/sbin/sendmail -t <<ERRMAIL
To: ${1}
From: Systemd <root@${HOSTNAME}>
Subject: Service ${SERVICE} on ${HOSTNAME} has changed
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8
Importance: high
X-Priority: 1 (Highest)
X-MSMail-Priority: High
Service ${SERVICE} on ${HOSTNAME}
${NOTIFICATION_STATUS}
--------------------------------------------
After waiting ${SLEEP_INTERVAL} ${SERVICE} ${HOSTNAME} is now:
${FOLLOWUP_STATUS}
ERRMAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment