From f9c80534ea015ac73c8847c56be27f39246a55d0 Mon Sep 17 00:00:00 2001 From: techartdev Date: Thu, 5 Feb 2026 12:24:55 +0200 Subject: [PATCH] Implement PID file management for ttyd to prevent port conflicts --- openclaw_assistant/run.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/openclaw_assistant/run.sh b/openclaw_assistant/run.sh index dd06f69..ffa804d 100644 --- a/openclaw_assistant/run.sh +++ b/openclaw_assistant/run.sh @@ -252,13 +252,27 @@ openclaw gateway run & GW_PID=$! # Start web terminal (optional) -# Kill any stray ttyd processes from previous runs to avoid port conflicts -pkill -f "ttyd.*-p.*-b /terminal" || true +TTYD_PID_FILE="/var/run/openclaw-ttyd.pid" + +# Clean up stale ttyd process from previous run using PID file +if [ -f "$TTYD_PID_FILE" ]; then + OLD_PID=$(cat "$TTYD_PID_FILE" 2>/dev/null || echo "") + if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then + echo "Stopping previous ttyd process (PID $OLD_PID)..." + kill "$OLD_PID" 2>/dev/null || true + sleep 1 + # Force kill if still running + kill -9 "$OLD_PID" 2>/dev/null || true + fi + rm -f "$TTYD_PID_FILE" +fi if [ "$ENABLE_TERMINAL" = "true" ] || [ "$ENABLE_TERMINAL" = "1" ]; then echo "Starting web terminal (ttyd) on 127.0.0.1:${TERMINAL_PORT} ..." ttyd -W -i 127.0.0.1 -p "${TERMINAL_PORT}" -b /terminal bash & TTYD_PID=$! + echo "$TTYD_PID" > "$TTYD_PID_FILE" + echo "ttyd started with PID $TTYD_PID" else echo "Terminal disabled (enable_terminal=$ENABLE_TERMINAL)" fi