#!/bin/bash # Clawdbot Gateway wrapper for systemd # Handles port conflicts by killing orphaned processes before starting export CLAWDBOT_SKIP_USER_SERVICE_CHECK=1 PORT=18789 # 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 # Check if it's us (systemd-managed) or an orphan EXISTING_PPID=$(ps -o ppid= -p "$EXISTING_PID" 2>/dev/null | tr -d ' ') if [ "$EXISTING_PPID" != "1" ]; then echo "Killing orphaned gateway process PID=$EXISTING_PID (PPID=$EXISTING_PPID)" kill "$EXISTING_PID" 2>/dev/null sleep 2 # Force kill if still alive 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 --bind loopback "$@"