< Back to all hacks

#52 Termux:Boot Auto-Start

Boot
Problem
Phone reboot means manual restart of gateway, sshd, crons. Not viable for autonomous operation.
Solution
Termux:Boot fires BOOT_COMPLETED intent, runs ~/.termux/boot/start-pocketclaw.sh automatically.
Lesson
Termux:Boot must be opened once after install to register the BOOT_COMPLETED receiver.

Context

PocketClaw needs to survive phone reboots without manual intervention. On a normal Android device, services restart automatically via the system. But our gateway runs inside Termux, which is just a user-space app. When the phone reboots, Termux starts but nothing inside it runs automatically.

Without auto-start, every reboot requires: opening Termux, starting sshd, starting the gateway, enabling cron jobs. On a phone with 1 GB RAM that occasionally gets OOM-killed and rebooted, this is unacceptable for a device meant to run headless as a server.

Prerequisites

  • Termux installed and configured
  • Termux:Boot app from F-Droid (NOT from Google Play — the Play Store version is outdated)
  • Gateway scripts already working (Hack #10, #33)

Implementation

Install Termux:Boot and open it once to register the Android BOOT_COMPLETED broadcast receiver. Then create the boot script:

# Install Termux:Boot from F-Droid
# IMPORTANT: Open the app once after install to register the receiver

# Create boot directory
mkdir -p ~/.termux/boot

Create the boot script that starts all services:

# Create the boot script
cat > ~/.termux/boot/start-pocketclaw.sh << 'SCRIPT'
#!/data/data/com.termux/files/usr/bin/bash
# PocketClaw boot sequence — runs on BOOT_COMPLETED

# Acquire wake lock to prevent Doze
termux-wake-lock

# Wait for WiFi to come up
sleep 15

# Start sshd for remote access
sshd

# Start cron daemon for scheduled tasks
crond

# Start the gateway via setsid (detached from terminal)
/system/bin/setsid start-openclaw > "$PREFIX/tmp/openclaw-gateway.log" 2>&1 &

# Start health monitor
nohup pocketclaw monitor > /dev/null 2>&1 &
SCRIPT

chmod +x ~/.termux/boot/start-pocketclaw.sh

Verification

# Reboot the phone:
adb reboot

# Wait 30-60 seconds, then check if sshd is running:
adb forward tcp:8022 tcp:8022
ssh -p 8022 -i ~/.ssh/id_moto localhost "pgrep sshd"
# Expected: PID numbers

# Check if gateway is running:
ssh -p 8022 -i ~/.ssh/id_moto localhost "pgrep -f openclaw-gateway"
# Expected: PID number

# Check gateway logs:
ssh -p 8022 -i ~/.ssh/id_moto localhost "tail -5 \$PREFIX/tmp/openclaw-gateway.log"
# Expected: Gateway startup messages

Gotchas

  • Termux:Boot MUST be opened once after install — just installing it is not enough
  • The sleep 15 is critical — WiFi takes 10-15 seconds to connect after boot. Starting the gateway before WiFi is up causes connection failures
  • The boot script runs as the Termux user (uid 10001), not root. Dirty COW daemon stopper must be run separately from ADB shell
  • If the phone has a PIN lock, Termux:Boot still fires but network may not be available until the device is unlocked (depends on Android version)
  • Termux:Boot scripts must have the shebang pointing to the Termux bash, not /bin/bash

Result

MetricBeforeAfter
Manual steps after reboot5+ commands0
Time to gateway ready3-5 min (manual)~30s (auto)
Survives OOM rebootNoYes