#!/bin/bash # OpenClaw Connection Repair Watcher LOG_FILE="$HOME/.openclaw/logs/repair.log" HEALTH_URL="http://100.64.0.39:18789/health" REMOTE_HOST="100.64.0.39" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') log() { echo "[$TIMESTAMP] $1" >> "$LOG_FILE" } check_health() { # Attempt to fetch health status with a short timeout HEALTH_STATUS=$(curl -s -m 5 "$HEALTH_URL") if [[ "$HEALTH_STATUS" == *'"ok":true'* ]]; then return 0 else return 1 fi } check_remote() { # Check if remote host is reachable via Tailscale if ping -c 1 -W 2 "$REMOTE_HOST" > /dev/null 2>&1; then return 0 else return 1 fi } repair() { log "Connection loss detected. Attempting repair..." # Restart SSH tunnel if it exists if launchctl list | grep -q ai.openclaw.ssh-tunnel; then log "Restarting ai.openclaw.ssh-tunnel..." launchctl stop ai.openclaw.ssh-tunnel 2>/dev/null launchctl start ai.openclaw.ssh-tunnel sleep 5 fi # Restart OpenClaw node log "Restarting ai.openclaw.node..." launchctl stop ai.openclaw.node 2>/dev/null launchctl start ai.openclaw.node log "Repair attempt completed." } # Main Watcher Logic log "Starting health check..." if ! check_health; then log "Health check failed for $HEALTH_URL" if ! check_remote; then log "Remote host $REMOTE_HOST is unreachable. Is Tailscale connected?" else repair fi else # Connection is healthy, log and exit log "Health check passed. Connection is healthy." exit 0 fi