How to Add Boot Alert Notification on Debian 12

Post Reply
User avatar
webxtek
Posts: 66
Joined: Wed Nov 18, 2020 7:43 pm
Been thanked: 2 times

This tutorial shows how to set up a **boot alert notification** script on a Debian 12 server with MyVesta, using `msmtp` to send email alerts. The script notifies you by email when the server successfully boots, including the hostname, public IP, and timestamp.

---

Step 1: Install and Configure msmtp
Install `msmtp` to handle email sending:

Code: Select all

apt update
apt install msmtp -y
Create the configuration file:

Code: Select all

nano /etc/msmtprc
Add the following configuration, replacing placeholders with your SMTP details:

Code: Select all

defaults
auth           on
tls            on
tls_starttls   off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        /var/log/msmtp.log

account        default
host           mail.yourdomain.com
port           465
from           [[email protected]](mailto:[email protected])
user           [[email protected]](mailto:[email protected])
password       YourSecurePassword
Secure the file:

Code: Select all

chmod 600 /etc/msmtprc
chown root:root /etc/msmtprc
Test with:

Code: Select all

echo -e "Subject: TestnnThis is a test email." | msmtp [[email protected]](mailto:[email protected])
Check `/var/log/msmtp.log` for errors.

---

Step 2: Create the Boot Notification Script
Create the script:

Code: Select all

nano /usr/local/bin/boot-notify.sh
Paste the following (replace placeholders):

Code: Select all

#!/bin/bash
sleep 10

TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
HOSTNAME=$(hostname)
IP_ADDRESS=$(curl -4 -s ifconfig.me)
MESSAGE_ID="boot-notify-$(date +%s)@$HOSTNAME"
DATE=$(date -R)

echo "[$TIMESTAMP] Executing boot notification script" >> /var/log/boot-notify.log

[ -z "$IP_ADDRESS" ] && {
IP_ADDRESS="Not available"
echo "[$TIMESTAMP] Warning: Could not retrieve public IP." >> /var/log/boot-notify.log
}

cat << EOF > /tmp/boot-notify-message.txt
Subject: $HOSTNAME Booted Successfully
From: Server Notification [[email protected]](mailto:[email protected])
Reply-To: [[email protected]](mailto:[email protected])
Date: $DATE
Message-ID: <$MESSAGE_ID>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>

<html><body style="font-family: Arial; background: #f4f4f9; padding: 20px;">
<div style="max-width: 600px; margin: auto; background: white; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
  <div style="background: #4CAF50; color: white; padding: 15px; text-align: center;">
    <h2>Server Boot Notification</h2>
  </div>
  <div style="padding: 20px;">
    <p><strong>Host:</strong> $HOSTNAME ($IP_ADDRESS)</p>
    <p><strong>Status:</strong> Successfully booted</p>
    <p><strong>Time:</strong> $TIMESTAMP</p>
  </div>
  <div style="background: #f4f4f9; padding: 10px; text-align: center; font-size: 12px; color: #777;">
    This is an automated notification from your server.
  </div>
</div>
</body></html>
EOF

msmtp [[email protected]](mailto:[email protected]) < /tmp/boot-notify-message.txt >> /var/log/boot-notify.log 2>&1
STATUS=$?
[ $STATUS -eq 0 ] && echo "[$TIMESTAMP] Email sent." >> /var/log/boot-notify.log || echo "[$TIMESTAMP] Email failed. Status: $STATUS" >> /var/log/boot-notify.log
rm -f /tmp/boot-notify-message.txt
exit 0
Make it executable:

Code: Select all

chmod +x /usr/local/bin/boot-notify.sh
---

Step 3: Create the systemd Service (Debian 12 Fix Applied)
Create service file:

Code: Select all

nano /etc/systemd/system/boot-notify.service
Add this (note proper `[Unit]`, `[Service]`, `[Install]` blocks):

Code: Select all

[Unit]
Description=Send boot email notification
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/boot-notify.sh

[Install]
WantedBy=multi-user.target
Enable the service:

Code: Select all

systemctl daemon-reexec
systemctl enable boot-notify.service
---

Step 4: Test It
Reboot:

Code: Select all

reboot
Then check the log:

Code: Select all

cat /var/log/boot-notify.log
You should receive a formatted HTML email with server info.

Let me know if it fails and I’ll help debug :mrgreen:
Post Reply