guide #053

Boot Script with WiFi Retry

Problem
Sleep 15 for WiFi is fragile. If WiFi takes 30s, everything fails silently.

Solution
Retry loop: ping every 5s, up to 12 attempts (60s max). Boot logging with timestamps. Graceful degradation.

Context

The original Termux boot script (Hack #52) used sleep 15 to wait for WiFi before starting the gateway. This works most of the time — WiFi typically connects in 10-15 seconds. But on cold boot after a crash, WiFi can take 30-45 seconds. On those occasions, the gateway starts without network and fails to connect to API providers, entering a broken state.

The fix is a retry loop that actively checks for connectivity before proceeding, with graceful degradation if WiFi never comes up.

Implementation

Replace the fixed sleep with an active ping-based retry loop:

#!/data/data/com.termux/files/usr/bin/bash
# start-pocketclaw.sh — robust boot with WiFi retry

LOG="$PREFIX/tmp/pocketclaw-boot.log"
log() { echo "$(date '+%H:%M:%S') $1" >> "$LOG"; }

log "Boot sequence started"

# Acquire wake lock
termux-wake-lock 2>/dev/null
log "Wake lock acquired"

# WiFi retry loop — 12 attempts x 5s = 60s max
RETRIES=12
WIFI_OK=0
for i in $(seq 1 $RETRIES); do
  if ping -c 1 -W 3 8.8.8.8 > /dev/null 2>&1; then
    log "WiFi ready after $i attempts"
    WIFI_OK=1
    break
  fi
  log "WiFi not ready, attempt $i/$RETRIES"
  sleep 5
done

if [ $WIFI_OK -eq 0 ]; then
  log "WARNING: WiFi not ready after $RETRIES attempts, continuing anyway"
fi

# Start sshd
sshd 2>/dev/null
log "sshd started"

# Start cron daemon
crond 2>/dev/null
log "crond started"

# Start gateway (setsid for detach)
/system/bin/setsid start-openclaw > "$PREFIX/tmp/openclaw-gateway.log" 2>&1 &
log "Gateway launched (PID: $!)"

log "Boot sequence complete"

Verification

# After reboot, check boot log:
cat $PREFIX/tmp/pocketclaw-boot.log
# Expected:
# 12:34:01 Boot sequence started
# 12:34:01 Wake lock acquired
# 12:34:06 WiFi not ready, attempt 1/12
# 12:34:11 WiFi not ready, attempt 2/12
# 12:34:16 WiFi ready after 3 attempts
# 12:34:16 sshd started
# 12:34:16 crond started
# 12:34:16 Gateway launched (PID: 1234)
# 12:34:16 Boot sequence complete

# Verify gateway is running:
pgrep -f openclaw-gateway
# Expected: PID number

Gotchas

  • Ping target 8.8.8.8 requires internet access, not just WiFi connection. If the router is up but WAN is down, the loop will fail all 12 attempts
  • The gateway starts even if WiFi never comes up (graceful degradation). The WiFi watchdog (Hack #46) will handle reconnection
  • Boot log is written to $PREFIX/tmp/ which is tmpfs and cleared on reboot. This is intentional — we don't want logs accumulating on limited storage
  • The 60-second maximum wait (12 x 5s) is designed for the worst case. Most boots complete WiFi check in 10-20 seconds

Result

MetricBeforeAfter
WiFi waitFixed 15sAdaptive 5-60s
Boot reliability~85%~99%
Failure modeSilent (broken gateway)Logged + graceful degradation
Boot time (normal)15s (fixed)5-15s (adaptive)
Continue reading
guide
Pocket AI complete guide
Running self-hosted AI on portable hardware
guide
Edge AI hardware buyer's guide 2026
Pi 5 vs Mini PC vs Mac Mini
report
Self-hosted AI landscape 2026
Quarterly state of the ecosystem
section
Pocket AI hardware hub
All portable hosts reviewed
section
Agent tracker
Live stats on every agent
newsletter
Thursday digest
Weekly summary in your inbox