Email boot notification with systemd
I have all sorts of machine/server that run either at home or elsewhere and I like to be informed when the system has booted. This can be quite useful for personal NAS when a power outage occurs or when you mannualy reboot the system remotely.
So first, let's write a simple script that send an email. For that you need to have working sendmail
command. I like to put my scripts in /opt/scripts/
, let write this one in /opt/scripts/boot-notification.sh
#! /bin/bash
echo -e "Subject: $HOSTNAME has booted up\n\nBooted at $(date)\n | sendmail [email protected]
Then, we make it executable with chmod
sudo chmod +x /opt/scripts/boot-notification.sh
At this point, you can try to run the script and check that you actually receive the mail.
Now, to run it at boot, it's just a matter of writing a service file for systemd. For that will write this one in /etc/systemd/system/boot-notification.service
[Unit]
Description=Boot notification
[Service]
ExecStart=/opt/scripts/boot-notification.sh
[Install]
WantedBy=multi-user.target
And not forget to enable it
systemctl enable boot-notification.service