Get a systemd Email on Failure Without a Mail Server
You wrote the hook. You tested it. Then the first real failure came and no mail ever showed up.
That's the usual story. The systemd part is easy. The hard part is getting a server to send mail that actually lands, since many clouds block outbound port 25 by default.
Skip the mail setup
FailNote emails you when any service fails, with the cause. No relay, no SMTP password on the box.
Option 1: OnFailure= plus a mail command
systemd can start another unit when a unit enters the failed state. That's the OnFailure= setting. You point it at a small template service that mails the status and logs.
1. Write the mail script
Save this as /usr/local/bin/mail-failure and put your address on the To: line.
#!/bin/sh
# Usage: mail-failure <unit>
unit="$1"
host="$(hostname)"
{
printf 'To: [email protected]\nSubject: [%s] %s failed\n\n' "$host" "$unit"
systemctl status --full --no-pager "$unit"
echo
journalctl -u "$unit" -n 40 --no-pager
} | /usr/sbin/sendmail -t Make it executable with sudo chmod 755 /usr/local/bin/mail-failure.
2. Add the template unit
Create /etc/systemd/system/[email protected]:
[Unit]
Description=Mail a failure report for %i
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mail-failure %i 3. Hook it into a service
Run sudo systemctl edit backup.service and add:
[Unit]
OnFailure=mail-failure@%N.service To cover every service at once instead, use a top-level drop-in. Our guide to a systemd alert on failure shows that setup, including the step that stops the alert unit from triggering itself.
Give the server a way to send mail
The script above needs a working sendmail. You don't need a full mail server for that. A small relay client like
msmtp hands each message to a real mail provider over port 587.
On Debian or Ubuntu, msmtp-mta installs msmtp and makes it answer as sendmail:
sudo apt install msmtp-mta Then create /etc/msmtprc with your provider's SMTP details:
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account alerts
host smtp.example.com
port 587
from [email protected]
user [email protected]
password YOUR_SMTP_PASSWORD
account default : alerts
Lock it down with sudo chmod 600 /etc/msmtprc, since it holds a password. Send yourself a test before you trust it:
printf 'Subject: relay test\n\nIt works.\n' | sendmail [email protected]
On openSUSE there's a packaged version of this whole idea.
systemd-status-mail ships the
template unit and reads its address and mailer from /etc/default/systemd-status-mail.
Test it with a service that always fails
printf '[Unit]\nOnFailure=mail-failure@%%N.service\n[Service]\nExecStart=/bin/false\n' | sudo tee /etc/systemd/system/mail-test.service
sudo systemctl daemon-reload
sudo systemctl start mail-test.service
systemctl status [email protected]
The mail should arrive within a minute. If it doesn't, the status output and /var/log/msmtp.log show where it stopped.
Remove the test unit afterward and run sudo systemctl reset-failed mail-test.service.
Where mail from the server lets you down
- Port 25 is often closed. AWS throttles it and Google Cloud blocks it, so plain local delivery rarely leaves the box.
- Every server holds a mail password. Ten servers means ten copies of an SMTP login to rotate and protect.
- Mail can land in spam. A sender domain without SPF and DKIM set up for that relay often goes to junk, or gets dropped.
- A dead server sends nothing. If the machine hangs or loses its network, the hook never runs.
- Restart loops flood the inbox. A service with
Restart=can fail over and over, and each failure can send another mail. - You get logs, not answers. The mail shows raw status output. You still work out the cause yourself.
Option 2: let FailNote send the mail
FailNote is a small agent for Linux servers. One install command covers every service on the machine. When a unit fails, the agent reports it to our servers, and we send the email, so nothing on your box talks SMTP.
curl -fsSL https://app.itadminutils.com/install.sh | sudo sh -s -- YOUR_KEY Your dashboard shows the command with your key filled in. The agent is one shell script you can read before you run it.
- Mail that arrives. Alerts come from our sending domain, which is set up for delivery. No relay on the server.
- The cause, not just the logs. Each alert says what likely went wrong and the next command to run.
- One mail per crash loop. Repeat failures add to one incident, then you get a single recovery note.
- Silent servers get flagged. If a server stops checking in for five minutes, you hear about it.
- More than email. Slack, Discord, Microsoft Teams and webhooks work too, if your team lives there.
Here's the kind of email you get:
FAILED: backup.service on web-01
Result: exit-code (exited, status=203/EXEC)
Cause: systemd could not execute the configured command (Permission denied)
Check: file /opt/backup/run.sh && namei -l /opt/backup/run.sh To see every likely cause for an error like this, paste the full status into our free systemd failure checker.
Which one to pick
If you run one or two servers and already have a working mail relay, Option 1 is fine and free. Test it every few months, because relay passwords and provider rules change.
If mail can't leave your servers, or you want the cause in the alert, FailNote is quicker. It's free for one server, and Pro covers up to 10, billed monthly. Details are on the FailNote pricing section.
Mail is only one piece. Backups on timers need a missed-run check too, and our comparison of monitoring tools covers Prometheus, Zabbix and the rest.
Start FailNote free
Enter your email. We'll send a sign-in link, and your dashboard has the install command.
Already have an account? Sign in. See our privacy policy and terms.