#!/bin/bash # Clawdbot Builder Gateway wrapper for systemd # Runs on port 18790 (separate from Rivet on 18789) export CLAWDBOT_SKIP_USER_SERVICE_CHECK=1 export CLAWDBOT_STATE_DIR=/root/.clawdbot-builder PORT=18790 # Check if something is already holding the port EXISTING_PID=$(ss -tlnp "sport = :${PORT}" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | head -1) if [ -n "$EXISTING_PID" ]; then EXISTING_PPID=$(ps -o ppid= -p "$EXISTING_PID" 2>/dev/null | tr -d ' ') if [ "$EXISTING_PPID" != "1" ]; then echo "Killing orphaned builder gateway PID=$EXISTING_PID (PPID=$EXISTING_PPID)" kill "$EXISTING_PID" 2>/dev/null sleep 2 if kill -0 "$EXISTING_PID" 2>/dev/null; then echo "Force killing PID=$EXISTING_PID" kill -9 "$EXISTING_PID" 2>/dev/null sleep 1 fi else echo "Port $PORT held by systemd-managed PID=$EXISTING_PID — stopping" exit 1 fi fi exec /usr/bin/openclaw gateway run --port $PORT --bind loopback "$@"