Automating Server Wake-on-LAN After UPS-Triggered Shutdown When Mains Power Returns


9 views

When dealing with UPS-protected Linux servers, we often configure them to gracefully shut down during prolonged power outages. The common APC UPS setup uses USB communication with apcupsd for this purpose. However, there's a gap in functionality when mains power returns before UPS battery depletion - the server remains offline despite available power.

The typical BIOS "AC Power Recovery" setting works for direct power loss scenarios but fails here because:

  • The UPS maintains continuous power to servers
  • No actual power interruption occurs to trigger auto-wake
  • Wake-on-LAN (WoL) isn't automatically sent after shutdown

Here's a reliable approach using a Raspberry Pi as a power monitoring device:

# Raspberry Pi power monitor script (Python)
import RPi.GPIO as GPIO
import subprocess
import time

GPIO.setmode(GPIO.BCM)
POWER_PIN = 17  # GPIO pin connected to mains power sensor

def send_wol(mac):
    subprocess.call(['wakeonlan', mac])

def main():
    GPIO.setup(POWER_PIN, GPIO.IN)
    last_state = GPIO.input(POWER_PIN)
    
    while True:
        current_state = GPIO.input(POWER_PIN)
        if last_state == 0 and current_state == 1:  # Power restored
            send_wol('00:11:22:33:44:55')  # Replace with server MAC
        last_state = current_state
        time.sleep(5)

if __name__ == "__main__":
    main()

Hardware Requirements:

  • Raspberry Pi (any model with GPIO)
  • AC power sensor (like HiLetgo ACS712)
  • Network connection to target servers

Configuration Steps:

  1. Install wakeonlan on the Pi: sudo apt install wakeonlan
  2. Enable WoL on target servers: ethtool -s eth0 wol g
  3. Configure apcupsd to not shutdown for brief outages

For environments without spare hardware, consider these options:

# Cron job on a always-on device to ping UPS and wake servers
#!/bin/bash
UPS_IP="192.168.1.100"
SERVER_MAC="00:11:22:33:44:55"

if ping -c 1 $UPS_IP &> /dev/null; then
    # UPS is back online
    wakeonlan $SERVER_MAC
fi

Common issues and fixes:

  • WoL not working: Verify NIC settings with ethtool eth0
  • False triggers: Add debounce logic to power monitoring
  • Network issues: Use static ARP entries for target servers

When dealing with UPS-protected Linux servers, we often encounter this scenario: The UPS properly triggers shutdown during extended power loss, but fails to restart servers when mains power returns before UPS battery depletion. The server's AC power sensing can't detect restoration since the UPS maintains continuous power output.

Here's a proven solution using Wake-on-LAN (WoL) with a power-state monitoring device:

Power Grid → [Monitoring Device] → Network Switch → [Target Servers]
                 ↑
UPS Bypass Line →'

A Raspberry Pi with a relay module makes an excellent monitoring device. Sample Python code for power detection:

import RPi.GPIO as GPIO
import subprocess
import time

POWER_PIN = 17
SERVERS = ["192.168.1.10", "192.168.1.11"]
MAC_ADDRESSES = ["00:11:22:33:44:55", "00:11:22:33:44:56"]

GPIO.setmode(GPIO.BCM)
GPIO.setup(POWER_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def send_wol(mac):
    subprocess.call(["wakeonlan", mac])

while True:
    if GPIO.input(POWER_PIN):
        for i, server in enumerate(SERVERS):
            # Verify server is offline before sending WoL
            response = os.system(f"ping -c 1 {server}")
            if response != 0:
                send_wol(MAC_ADDRESSES[i])
    time.sleep(60)

For APC UPS users, we can extend apcupsd configuration. Add to /etc/apcupsd/apccontrol:

#!/bin/bash
case $1 in
    "powerback")
        /usr/bin/wakeonlan 00:11:22:33:44:55
        ;;
esac

For servers with BMC/IPMI:

# Set chassis policy to always-on
ipmitool chassis policy always-on

# Power on via IPMI when power restored
ipmitool -H 192.168.1.10 -U admin -P password power on

Commercial solutions include:

  • Digital Loggers Web Power Switch (with power sensing API)
  • Ubiquiti mPower with RESTful control
  • Sonoff POW R2 with Tasmota firmware