start-debug-safe.shā¢1.94 kB
#!/bin/bash
# Safest way to start debug - avoids race conditions
# This script aggressively cleans and uses longer waits
set -e
echo "š”ļø SAFE DEBUG START š”ļø"
echo ""
# Step 1: Nuclear cleanup
echo "1ļøā£ Aggressive cleanup..."
pkill -9 -f "http-mcp-server.js" 2>/dev/null && echo " ā Killed server processes" || echo " ā No server processes"
pm2 stop mcp-sap-notes 2>/dev/null && echo " ā Stopped PM2" || echo " ā No PM2 process"
# Step 2: Wait and verify multiple times
echo "2ļøā£ Waiting for port 3123 to be completely free..."
for i in {1..15}; do
if ! lsof -ti :3123 &>/dev/null; then
echo " ā
Port 3123 is free (checked $i times)"
break
fi
PID=$(lsof -ti :3123 2>/dev/null || echo "")
if [ -n "$PID" ]; then
echo " ā ļø Attempt $i: Found PID $PID, killing..."
kill -9 $PID 2>/dev/null
fi
sleep 2
done
# Step 3: Final verification
echo "3ļøā£ Final port verification..."
sleep 3
if lsof -ti :3123 &>/dev/null; then
echo "ā FAILED: Port 3123 is STILL in use after aggressive cleanup"
echo ""
echo "Current processes on port 3123:"
lsof -i :3123
echo ""
echo "š” Something is persistently binding to this port."
echo " Try using a different port by setting HTTP_PORT=3124 in .env"
exit 1
fi
echo " ā
Port 3123 is FREE and stable"
# Step 4: Pre-build (outside of debug script to avoid timing issues)
echo "4ļøā£ Building project..."
npm run build --silent
# Step 5: One more check before starting
echo "5ļøā£ Pre-flight check..."
sleep 1
if lsof -ti :3123 &>/dev/null; then
echo "ā Port was grabbed during build!"
kill -9 $(lsof -ti :3123)
sleep 2
fi
# Step 6: Start server directly (skip npm to avoid spawning issues)
echo "6ļøā£ Starting debug server..."
echo ""
LOG_LEVEL=debug HTTP_PORT=3123 AUTO_START=true DEBUG_START=true node dist/http-mcp-server.js