Bachelor Tech
  • Home
  • Tutorials
  • Tips
  • Portfolio
  • About Jan
  • Contact Jan

4. Prepare onbaterry and offbatery APC scripts

by Jan Bachelor October 28, 2025

In case you did not know, when power goes down and the UPS switches to battery power, the apcupsd service automatically triggers a shell script under /etc/apcupsd/onbattery . We will need to modify it to use our mail service and email the right email address.

nano /etc/apcupsd/onbattery

#!/bin/bash

# Variables
MAIL_TO="[email protected]"
LOG_FILE="/var/log/ups_manager.log"
MAIL_BODY="/tmp/power_lost.html"
SUBJ="Power LOST for `hostname`"

# Create HTML email body
cat > $MAIL_BODY << EOF
<html>
	<body>
		<h2>Power was lost!</h2>
		<p><strong>Current APC status:</strong>
		<pre>`/usr/sbin/apcaccess status`</pre>
		<p><strong>Recent log file output:</strong>
		<pre>`tail -n 20 /var/log/ups_manager.log`</pre>
		<p>Your RPi script :)</p>
	</body>
</html>
EOF

mutt -e 'set content_type="text/html"' \\
     -s "$SUBJ" \\
     "$MAIL_TO" \\
     -a "$LOG_FILE" < "$MAIL_BODY"

rm -f "$MAIL_BODY"  # Remove our temporary file
exit 0
Screenshot of the script as shown in the nano text editor to configure the onbaterry script.
Set up rich text email notification when UPS switches on battery power during power loss
  • Once power is back on, another script under /etc/apcupsd/offbattery is executed. Located it and at the end of it, please add the following lines to clear the flag for our script.
  • Kudos to https://pieterbakker.com/using-mutt-to-send-html-emails-with-attachments/ for the suggestions on how to use HTML format with mutt.
nano /etc/apcupsd/offbattery

#!/bin/bash

# Variables
MAIL_TO="[email protected]"
LOG_FILE="/var/log/ups_manager.log"
MAIL_BODY="/tmp/power_restored.html"
SUBJ="Power restored for `hostname`"

# Clear the flags for future cases
rm -f /tmp/ups_shutdown_flags/*.flag

# Create HTML email body
cat > $MAIL_BODY << EOF
<html>
	<body>
		<h2>Good news, power was restored!</h2>
		<p><strong>APC status just after restoration:</strong>
		<pre>`/usr/sbin/apcaccess status`</pre>
		<p><strong>Recent log file output:</strong>
		<pre>`tail -n 20 /var/log/ups_manager.log`</pre>
		<p>Your RPi script :)</p>
	</body>
</html>
EOF

mutt -e 'set content_type="text/html"' \\
     -s "$SUBJ" \\
     "$MAIL_TO" \\
     -a "$LOG_FILE" < "$MAIL_BODY"

rm -f "$MAIL_BODY"  # Remove our temporary file
exit 0

Screenshot of the script as shown in the nano text editor to configure the offbaterry script.
Similarly, set up rich text emails once off battery (back to power) + remove shutdown flags
  • Make the scripts executable and test them:
sudo chmod +x /usr/local/sbin/ups_manager.sh
sudo chmod +x /etc/apcupsd/onbattery
sudo chmod +x /etc/apcupsd/offbattery

# Calling these two should result in an email being sent.
bash /etc/apcupsd/onbattery
bash /etc/apcupsd/offbattery
# Should show you that the UPS is connected to power and no further action is needed.
bash /usr/local/sbin/ups_manager.sh
  • Schedule it to run every two minutes:
sudo crontab -e

# Add this line:
*/2 * * * * /usr/local/sbin/ups_manager.sh
  • Note that logs will be saved under /var/log/ups_manager.log in addition to the email alerts. At this point, if the power goes down, you will receive an email and the script will trigger further to eventually start gracefully shutting down your Proxmox hosts.
  • One more thing, we need to ensure that the log gets rotated, as it will be getting updates every two minutes and will clog up quickly. Let’s use logrotate for that.
sudo nano /etc/logrotate.d/ups_manager

/var/log/ups_manager.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}

# Test it
sudo logrotate -d /etc/logrotate.conf

Are we finally ready for a test? Yes! But how about we configure a script also for our existing monitoring system, such as Uptime Kuma, first?

3. Prepare a Script of Scripts for automated shutdown of Proxmox hosts
5. Bonus: Add UPS monitoring to Uptime Kuma
Go back to course overview: Automate Graceful Shutdown & Wake on LAN for Proxmox with RPi (APC)

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 FacebookWhatsappEmail

Course Steps

  1. 1. Automated Proxmox UPS Shutdown Process - Goals & Installation
  2. 2. Configure apcupsd, SSH keys and SMTP
  3. 3. Prepare a Script of Scripts for automated shutdown of Proxmox hosts
  4. 4. Prepare onbaterry and offbatery APC scripts
  5. 5. Bonus: Add UPS monitoring to Uptime Kuma
  6. 6. Run a UPS drain test!
  7. 7. Add Wake-on-LAN to automatically wake up your hosts!
  8. 8. GitOps - manage your shutdown scripts with Gitea

Other courses

Create an automated Gravity workflow that will allow...

January 19, 2024

Dynamically Populate Gravity Forms from Google Sheets (GSheets...

March 16, 2021

Concur Alternative: Import Employees’ Credit Card Expenses to...

January 19, 2024

Turn your Raspberry Pi into a Proxmox Backup...

July 13, 2025

Install iRedMail Mail Server As Proxmox VM With...

October 31, 2024

Recent Posts

  • How to get LXC containers for Proxmox outside of ready-made templates

  • How to join two Proxmox nodes into a cluster (PVE 8.x)

  • How to Rename a Proxmox Node

Facebook Twitter Instagram Pinterest Linkedin Youtube

All Rights Reserved. The theme was customized from Soledad, see PenciDesign

Bachelor Tech
  • Home
  • Tutorials
  • Tips
  • Portfolio
  • About Jan
  • Contact Jan