#!/bin/bash

# 修复端口冲突脚本

PORT=8080

echo "=== 检查端口 $PORT 占用情况 ==="

# 查找占用端口的进程
PID=$(lsof -ti:$PORT 2>/dev/null || netstat -tlnp 2>/dev/null | grep ":$PORT " | awk '{print $7}' | cut -d'/' -f1 | head -1)

if [ -n "$PID" ]; then
    echo "找到占用端口 $PORT 的进程: PID=$PID"
    
    # 显示进程信息
    ps aux | grep $PID | grep -v grep
    
    echo ""
    read -p "是否要停止该进程? (y/n): " confirm
    
    if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
        echo "正在停止进程 $PID..."
        kill -15 $PID
        
        # 等待进程结束
        sleep 2
        
        # 如果还在运行，强制杀死
        if ps -p $PID > /dev/null 2>&1; then
            echo "强制停止进程..."
            kill -9 $PID
        fi
        
        echo "进程已停止"
    else
        echo "取消操作"
        exit 1
    fi
else
    echo "未找到占用端口 $PORT 的进程"
fi

echo ""
echo "=== 现在可以重新启动应用 ==="
echo "./start.sh start"

