The remote server relies on 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 remote machine
- 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
-
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 remote 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 script to a value slightly less than this estimate, accounting for factors like battery aging and efficiency losses.
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