Skip to content

Instantly share code, notes, and snippets.

@vilhelmen
Last active June 27, 2016 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vilhelmen/28927e6aa2260cd94d015fc2fa19dfb0 to your computer and use it in GitHub Desktop.
Save vilhelmen/28927e6aa2260cd94d015fc2fa19dfb0 to your computer and use it in GitHub Desktop.
Email alert service for systemd
#!/bin/sh
LOG_DATA=$(mktemp --suffix='.log')
journalctl -o short --since -30min -u ${2} > ${LOG_DATA}
echo "Service ${2} has failed on ${1}. Log attached." | mutt -F mutt.cfg -s "${1} SERVICE FAILURE ALERT" -a ${LOG_LOC} $(cat recipients.cfg)
[Unit]
Description=Email alert service
# trigger with OnFailure=email-alert@%n.service
Requires=network-online.target
AssertFileNotEmpty=/opt/email-alert/mutt.cfg
AssertFileNotEmpty=/opt/email-alert/recipients.cfg
User=root
[Service]
Type=oneshot
PrivateTmp=true
PrivateDevices=true
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true
WorkingDirectory=/opt/email-alert/
ExecStart=/opt/email-alert/email-alert.sh %H %I
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment