Files
tilbudgivern/scripts/cleanup-ports.sh

50 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Port cleanup script for Tilbudgivern application
# Kills any processes using the specified ports before starting the app
set -e
PORTS=(4031 4032)
TIMEOUT=5
echo "[Port Cleanup] Starting port cleanup..."
for PORT in "${PORTS[@]}"; do
echo "[Port Cleanup] Checking port $PORT..."
# Check if port is in use
if command -v lsof &> /dev/null; then
PIDS=$(lsof -ti :$PORT 2>/dev/null || true)
if [ -n "$PIDS" ]; then
echo "[Port Cleanup] Found processes on port $PORT: $PIDS"
for PID in $PIDS; do
echo "[Port Cleanup] Killing process $PID on port $PORT..."
kill -9 "$PID" 2>/dev/null || true
done
else
echo "[Port Cleanup] Port $PORT is free"
fi
elif command -v netstat &> /dev/null; then
# Fallback to netstat if lsof not available
if netstat -tlnp 2>/dev/null | grep -q ":$PORT "; then
echo "[Port Cleanup] Port $PORT is in use (netstat)"
# Try to extract and kill the process
PID=$(netstat -tlnp 2>/dev/null | grep ":$PORT " | awk '{print $NF}' | cut -d'/' -f1 || true)
if [ -n "$PID" ] && [ "$PID" != "-" ]; then
echo "[Port Cleanup] Killing process $PID on port $PORT..."
kill -9 "$PID" 2>/dev/null || true
fi
else
echo "[Port Cleanup] Port $PORT is free"
fi
else
echo "[Port Cleanup] Warning: Neither lsof nor netstat available, skipping port check"
fi
done
echo "[Port Cleanup] Waiting $TIMEOUT seconds for ports to release..."
sleep $TIMEOUT
echo "[Port Cleanup] Port cleanup completed"