-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnut-shutdown.sh
More file actions
31 lines (27 loc) · 1.22 KB
/
Copy pathnut-shutdown.sh
File metadata and controls
31 lines (27 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/sh
# Host shutdown helper invoked by upsmon's SHUTDOWNCMD when
# SHUTDOWN_ON_BATTERY_CRITICAL=true. Tries D-Bus powerOff with retries;
# logs structured level lines for Alloy pickup.
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
readonly DBUS_MAX_RETRIES=3
readonly DBUS_RETRY_SLEEP=2
readonly DBUS_REPLY_TIMEOUT_MS=3000
printf 'level=error msg="UPS forced shutdown triggered; powering off host"\n' >&2
attempt=1
while [ "$attempt" -le "$DBUS_MAX_RETRIES" ]; do
if dbus-send --system --print-reply --reply-timeout="$DBUS_REPLY_TIMEOUT_MS" \
--dest=org.freedesktop.login1 /org/freedesktop/login1 \
org.freedesktop.login1.Manager.PowerOff boolean:false >&2; then
printf 'level=info msg="host poweroff dispatched via D-Bus" attempt=%d\n' "$attempt" >&2
exit 0
fi
if [ "$attempt" -lt "$DBUS_MAX_RETRIES" ]; then
printf 'level=warn msg="D-Bus poweroff failed, retrying" attempt=%d\n' "$attempt" >&2
sleep "$DBUS_RETRY_SLEEP"
fi
attempt=$((attempt + 1))
done
printf 'level=error msg="D-Bus poweroff failed after %d attempts; host will NOT shut down cleanly"\n' "$DBUS_MAX_RETRIES" >&2
exit 1