Your Agent Lost the Network Overnight. The Radio Just Went to Sleep.

You check in after an overnight run. systemctl status says the agent service is active. Your local healthcheck is green. But the job log tells a different story: every call that needed the internet timed out sometime after 2 a.m. The API client gave up, git fetch never finished, and the Discord summary never arrived.

So you look now. Ping works. Name lookups work. A fetch completes in a second. It is as if the network healed itself the moment you signed in.

It probably did, in a sense. On a lot of home Linux boxes, and especially on a Raspberry Pi using onboard Wi-Fi, the wireless radio is allowed to enter power save after idle time. Your SSH session is the traffic that woke it up.

This guide covers how to confirm the wireless interface is in power save, rule out the network cousins that look similar, turn power save off, persist the setting so a reconnect does not quietly put it back, and add a fail-closed preflight so the next job does not start against a sleeping radio.

The errors you’ll actually see

None of these name the radio. They look like generic network trouble:

  • API clients and curl time out after the box has been quiet for hours: curl: (28) Connection timed out after 10001 milliseconds.
  • git fetch hangs with no output, or fails with Could not resolve host: github.com.
  • Discord or other webhook posts are not sent, and the job log shows a timeout instead of an HTTP error.
  • Temporary failure in name resolution appears in the logs, then disappears as soon as you start poking the box.
  • The first ping fails or is slow, then the rest succeed, as if the first packet woke the link.
  • dmesg or the journal shows the station deauthenticating or disconnecting, sometimes with an inactivity reason.
  • iw dev wlan0 get power_save reports Power save: on.
  • The same thing happens the next quiet night.

Rule out the cousins first

Several other problems produce overlapping symptoms. Check these before you blame power save:

  • DNS only. If a ping to a raw IP such as 1.1.1.1 works and only names fail, that is a resolver problem, not a sleeping radio. See the DNS name resolution post.
  • No default route or a lapsed DHCP lease. If ip route show default is empty even while you are actively using the box, the problem is addressing, not radio sleep.
  • Undervoltage or throttling. A weak supply can also make Wi-Fi flaky on a Pi. If vcgencmd get_throttled is not 0x0, read the undervoltage and throttling post first.
  • The access point rebooted, or RF kill is set. Check your router’s uptime and run rfkill list. A soft or hard block is a different fix.
  • It was never the Wi-Fi path. If the default route goes out eth0, the Wi-Fi power-save setting is irrelevant to this failure.

Why it hides

Power save is often the default on Raspberry Pi onboard Wi-Fi and on many USB adapters, so nobody turned it on and nobody thinks to check it. Short interactive checks generate traffic, which wakes the radio, so the failure is gone by the time you look. systemd reports the agent service as active because the process is up. A healthcheck that only hits localhost never leaves the box, so it stays green. Some chips also re-enable power save when the interface reconnects, so a one-time iw command works until the next roam or access point restart.

Step 1: Confirm power save is on

Your wireless interface may not be named wlan0. Find it first:

iw dev
ip -br link
IFACE=$(iw dev | sed -n 's/^[[:space:]]*Interface //p' | head -n1)
echo "wireless interface: ${IFACE:-none}"

Then check its state, the route, and reachability:

iw dev "$IFACE" get power_save
iw dev "$IFACE" link
rfkill list
ip route show default

GW=$(ip -o route show default | sed -n 's/.* via \([^ ]*\).*/\1/p' | head -n1)
ping -c 3 -W 2 "$GW"
ping -c 3 -W 2 1.1.1.1
getent hosts api.github.com || echo "name lookup failed"

Then look for disconnects during the window when jobs failed:

journalctl --since "12 hours ago" | grep -Ei 'deauth|disassoc|disconnect|inactiv' | tail -n 20
sudo dmesg | grep -Ei 'deauth|disassoc|disconnect' | tail -n 20

Remember that your pings just generated traffic. “It pings now” does not prove the link was healthy at 3 a.m. The durable signal is the configured power-save state. If get power_save says on, the default route goes out that interface, and the overnight failures clustered after idle time, this is the likely cause.

Step 2: Immediate relief

sudo iw dev "$IFACE" set power_save off
iw dev "$IFACE" get power_save    # expect: Power save: off

Retry a fetch that failed overnight to confirm the path works. This is not the lasting fix. It lasts until the interface reconnects or the box reboots.

Step 3: Persist the setting

Raspberry Pi OS Bookworm typically uses NetworkManager. Its wifi.powersave values are easy to get backwards:

  • 0 = use the default (this is not off)
  • 1 = ignore, leave whatever the driver does
  • 2 = disable power save
  • 3 = enable power save

You want 2. Setting 3 because it “sounds like the strongest option” turns power save on.

Set a global default with a drop-in:

sudo tee /etc/NetworkManager/conf.d/wifi-powersave.conf >/dev/null <<'EOF'
[connection]
wifi.powersave = 2
EOF

Then set it on the specific connection too, so it does not depend on the default:

nmcli -t -f NAME,TYPE connection show --active
CONN="your-wifi-connection-name"
sudo nmcli connection modify "$CONN" 802-11-wireless.powersave 2
nmcli -g 802-11-wireless.powersave connection show "$CONN"   # expect 2

Restarting NetworkManager or bouncing the connection with nmcli connection up can drop your SSH session if you are connected over that same Wi-Fi. Don’t do that remotely. The stored setting applies on the next activation; for right now, rely on the iw command from step 2 and confirm with iw dev "$IFACE" get power_save.

Because some firmware turns power save back on at reconnect, add a dispatcher script that reapplies it every time a wireless interface comes up. A boot-time systemd oneshot alone is not enough, since it runs once and never sees the reconnect.

sudo tee /etc/NetworkManager/dispatcher.d/50-wifi-powersave-off >/dev/null <<'EOF'
#!/bin/sh
IFACE="${1}"
ACTION="${2}"
[ "$ACTION" = "up" ] || exit 0
[ -d "/sys/class/net/$IFACE/wireless" ] || exit 0
/usr/sbin/iw dev "$IFACE" set power_save off \
  && logger -t wifi-powersave "power save off on $IFACE"
EOF
sudo chown root:root /etc/NetworkManager/dispatcher.d/50-wifi-powersave-off
sudo chmod 755 /etc/NetworkManager/dispatcher.d/50-wifi-powersave-off

NetworkManager only runs dispatcher scripts that are owned by root and not writable by group or others, so the ownership and mode matter.

If the box still uses dhcpcd or ifupdown instead, the equivalents are wireless-power off in the interface stanza of /etc/network/interfaces, or a line in /etc/dhcpcd.exit-hook that runs iw dev "$interface" set power_save off when $reason is BOUND, REBOOT, or CARRIER. You don’t need to switch network stacks to fix this.

Don’t disable all power management on the Pi, edit firmware, or force a driver you do not have. And if iw reports off and the chip still drops off overnight, not every adapter honors the flag. In that case, the preflight below plus the association and route checks are what protect your jobs.

Step 4: Add a fail-closed preflight

Like the recent inode, EMFILE, and mountpoint preflights, this one either passes cleanly or stops the job with one clear log line. It only gates the Wi-Fi path: if there is no wireless interface, or the default route is on ethernet, it logs that and exits 0. If iw cannot be read for the wireless default-route interface, it fails closed.

Save it as /usr/local/bin/agent-wifi-preflight and chmod 755 it:

#!/usr/bin/env bash
# Fail closed if the default route is on Wi-Fi and the radio is asleep or unassociated.
set -u

TAG="wifi-preflight"
MARKER_DIR="${MARKER_DIR:-/run/agent-preflight}"
MARKER="$MARKER_DIR/wifi.alerted"
WEBHOOK="${DISCORD_WEBHOOK_URL:-}"
PROBE="${PROBE:-0}"

log() { logger -t "$TAG" -- "$1" 2>/dev/null; echo "$TAG: $1" >&2; }

notify() {
  [ -n "$WEBHOOK" ] || return 0
  safe="$(printf '%s' "$1" | tr -d '"\\')"
  curl -fsS -m 10 -H 'Content-Type: application/json' \
    -d "{\"content\":\"$safe\"}" "$WEBHOOK" >/dev/null 2>&1 \
    || log "webhook post failed"
}

fail() {
  log "FAIL $1"
  mkdir -p "$MARKER_DIR" 2>/dev/null || true
  if [ ! -e "$MARKER" ]; then
    notify "$(hostname): $TAG FAIL $1"
    touch "$MARKER" 2>/dev/null || log "cannot write marker in $MARKER_DIR"
  fi
  exit 1
}

mkdir -p "$MARKER_DIR" 2>/dev/null || true

WIFI="$(iw dev 2>/dev/null | sed -n 's/^[[:space:]]*Interface //p')"
if [ -z "$WIFI" ]; then
  log "OK no wireless interface; Wi-Fi checks skipped"
  exit 0
fi

DEV="$(ip -o route show default 2>/dev/null | sed -n 's/.* dev \([^ ]*\).*/\1/p' | head -n1)"
[ -n "$DEV" ] || fail "no default route (wireless present: $WIFI)"

if [ ! -d "/sys/class/net/$DEV/wireless" ]; then
  log "OK default route via $DEV (not Wi-Fi); Wi-Fi checks skipped"
  exit 0
fi

PS="$(iw dev "$DEV" get power_save 2>/dev/null)" || fail "cannot read power save on $DEV"
case "$PS" in
  *": off"*) ;;
  *": on"*)  fail "power save is ON on $DEV" ;;
  *)         fail "unexpected power save output on $DEV: $PS" ;;
esac

iw dev "$DEV" link 2>/dev/null | grep -q '^Connected to' \
  || fail "$DEV is not associated to an access point"

if [ "$PROBE" = "1" ]; then
  GW="$(ip -o route show default | sed -n 's/.* via \([^ ]*\).*/\1/p' | head -n1)"
  [ -n "$GW" ] && ping -c 1 -W 2 "$GW" >/dev/null 2>&1 \
    || fail "gateway ${GW:-unknown} not reachable via $DEV"
fi

if [ -e "$MARKER" ]; then
  rm -f "$MARKER"
  notify "$(hostname): $TAG recovered, $DEV healthy"
fi
log "OK $DEV power save off, associated, default route present"
exit 0

The webhook URL comes from the environment and is never printed. The marker lives under /run, so it clears on reboot and you get one alert per outage, not one per cron tick. If cron runs as a non-root user, create the marker directory at boot with a tmpfiles rule:

echo 'd /run/agent-preflight 0755 pi pi -' | sudo tee /etc/tmpfiles.d/agent-preflight.conf
sudo systemd-tmpfiles --create /etc/tmpfiles.d/agent-preflight.conf

The power-save and association checks are the main gate. The optional ping is off by default (PROBE=1 enables it) because a ping can itself wake a sleeping radio and hide the problem you are trying to catch.

Chain it in cron so the job only runs if the preflight passes:

15 2 * * * /usr/local/bin/agent-wifi-preflight && /opt/agent/run-nightly.sh

Or gate a systemd oneshot with ExecStartPre=:

[Service]
ExecStartPre=/usr/local/bin/agent-wifi-preflight
ExecStart=/opt/agent/run-nightly.sh

If the preflight fails, the job does not start, and the failure shows up in the journal with one readable line. Pair it with a stalled automation alarm so a skipped run is noticed even when nobody reads the journal.

Standing routine

  • Every job: run the preflight first, chained with &&.
  • After every reboot or Wi-Fi reconnect: run iw dev "$IFACE" get power_save and confirm the dispatcher log line in the journal.
  • After adding or swapping a USB Wi-Fi adapter, or changing the NetworkManager connection: find the interface name again, reapply 802-11-wireless.powersave 2 to the new connection, and rerun the preflight.
  • Monthly: confirm nmcli -g 802-11-wireless.powersave connection show "$CONN" still reports 2, and that iw still reports off.

If you run a reboot and power-loss test, add the power-save and association check to it. A box that comes back from power loss with the radio quietly in power save can pass every other check and still fail at 3 a.m.

If you want the prompts and checklists we use to keep unattended agents honest, start with the free prompts, go deeper with the OpenClaw Agent Ops Toolkit, or get everything in the Ultimate Bundle.

More from the build log

Suggested

Want the full MarketMai stack?

Get the core MarketMai guides and operator playbooks in one premium bundle for $49.

View Bundle