The in house server is powered by a basic UPS that lacks advanced monitoring capabilities. To ensure the server can be gracefully shut down during a power outage, we need an external method to monitor the power state by using the battery status of another device (e.g., laptop).
Benefits:
- No additional hardware needed
- Simple implementation
- Quick response to power outages
Considerations:
- Depends on the laptop being operational
- Single point of monitoring
- Network connectivity required
When executing sudo poweroff
, this assumes the hypervisor can gracefully shut down VMs (e.g., Proxmox). Make sure your hypervisor supports this.
- Linux with systemd
acpi
package (sudo apt install acpi
)- SSH key-based authentication configured
- Passwordless sudo on the UPS powered server
- Clone the repository and prepare the script:
git clone https://github.com/vwkyc/NoUPSPoweroff.git cd NoUPSPoweroff chmod +x NoUPSPoweroff.sh
-
Edit the configuration file
NoUPSPoweroff.conf
:- General Settings:
minutes
: Time to wait before shutdown when on battery (default: 25)sleep_interval
: Sleep interval between checks in seconds (default: 5)status_interval
: Status update interval in seconds (default: 120)min_battery
: Minimum battery percentage before immediate shutdown (default: 10)ac_stable_time
: AC power stability time before cancelling shutdown in seconds (default: 300 = 5 minutes)
- File Paths: All temporary files use unique
noupspoweroff_
prefixes to prevent conflicts - Servers: Define multiple servers to shutdown with
[serverN]
sections
- General Settings:
-
Edit username in
NoUPSPoweroff.service
-
Install the script and service:
sudo install -m 755 NoUPSPoweroff.sh /usr/local/bin/ sudo cp NoUPSPoweroff.service /etc/systemd/system/ sudo cp NoUPSPoweroff.conf /etc/ sudo systemctl daemon-reload sudo systemctl enable NoUPSPoweroff.service --now
Allow passwordless poweroff on the UPS powered server:
-
Allow the user
USERNAME
to executesudo poweroff
without a password:sudo visudo
-
Add the following line at the end:
USERNAME ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown
To determine the appropriate MINUTES
value for the script, you need to calculate the expected runtime of your server on the UPS. Follow these steps:
-
Identify Your Server's Power Consumption:
- Measure the power usage of your server in watts (W). This can be found in the server's documentation or by using a power meter.
-
Determine Your UPS Battery's Capacity:
- Find the battery's capacity, typically expressed in ampere-hours (Ah) and voltage (V). This information is usually provided by the UPS manufacturer.
-
Calculate the Battery's Total Energy Storage:
- Use the formula:
Battery Energy (Wh) = Battery Capacity (Ah) × Battery Voltage (V)
- Use the formula:
-
Estimate the UPS Runtime:
- Apply the formula:
Runtime (hours) = Battery Energy (Wh) / Server Power Consumption (W)
- Convert the runtime to minutes by multiplying by 60.
- Apply the formula:
Assume your server consumes 90 W, and your UPS has a battery capacity of 7.0 Ah at 12 V:
-
Calculate Total Battery Energy:
7.0 Ah × 12 V = 84 Wh
-
Determine Runtime:
Runtime (hours) = 84 Wh / 90 W = 0.933 hours = 56 minutes
In this scenario, the estimated runtime is approximately 56 minutes. To ensure a safe shutdown before the UPS battery is fully depleted, it's advisable to set the minutes
variable in your configuration to a value slightly less than this estimate, accounting for factors like battery aging and efficiency losses.
When AC power is restored after a power outage:
- Immediate Detection: The script detects AC power restoration immediately
- Stability Period: AC power must remain stable for 5 minutes (configurable via
ac_stable_time
) before cancelling any pending shutdown - Progress Updates: During the stability period, the script logs progress every check interval
- Power Flicker Protection: If power is lost again during the stability period, the timer resets
- Shutdown Cancellation: Only after the full stability period does the script cancel the pending shutdown
This prevents false cancellations from brief power flickers or unstable power conditions.
Monitor the service status and logs:
systemctl status NoUPSPoweroff.service
journalctl -u NoUPSPoweroff.service -f
-
Check permissions:
ls -l /usr/local/bin/NoUPSPoweroff.sh
-
Check logs:
journalctl -u NoUPSPoweroff.service -f
To disable and remove the NoUPSPoweroff service, follow these steps:
-
Stop and disable the service:
sudo systemctl stop NoUPSPoweroff.service sudo systemctl disable NoUPSPoweroff.service
-
Remove the service file and script:
sudo rm /etc/systemd/system/NoUPSPoweroff.service sudo rm /usr/local/bin/NoUPSPoweroff.sh
-
Reload the systemd daemon to apply changes:
sudo systemctl daemon-reload