Files
ai_wht_wechat/go_backend/stop.sh
2025-12-19 22:36:48 +08:00

105 lines
2.8 KiB
Bash

#!/bin/bash
#########################################
# AI小红书 Go 后端 - 停止服务脚本
#########################################
# 默认端口
DEV_PORT=8080
PROD_PORT=8070
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} 停止 AI小红书 Go 后端服务${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# 停止指定端口的服务
stop_port() {
local PORT=$1
echo -e "${YELLOW}正在停止端口 $PORT 上的服务...${NC}"
# 查找占用端口的进程
PORT_PID=$(lsof -ti:$PORT 2>/dev/null)
if [ -n "$PORT_PID" ]; then
echo " 找到进程: $PORT_PID"
kill -9 $PORT_PID 2>/dev/null && echo -e " ${GREEN}✅ 已终止进程 $PORT_PID${NC}"
else
echo -e " ${YELLOW}未找到占用端口 $PORT 的进程${NC}"
fi
# 使用 fuser 强制清理
sudo fuser -k $PORT/tcp 2>/dev/null || true
}
# 停止所有 go run main.go 进程
echo -e "${BLUE}=== 方法1: 停止 go run 进程 ===${NC}"
GO_PIDS=$(ps aux | grep "go run main.go" | grep -v grep | awk '{print $2}')
if [ -n "$GO_PIDS" ]; then
echo -e "${YELLOW}找到 Go 服务进程:${NC}"
ps aux | grep "go run main.go" | grep -v grep
echo ""
for PID in $GO_PIDS; do
kill -9 $PID 2>/dev/null && echo -e "${GREEN}✅ 已终止进程: $PID${NC}"
done
else
echo -e "${YELLOW}未找到 go run main.go 进程${NC}"
fi
echo ""
# 停止开发环境端口
echo -e "${BLUE}=== 方法2: 清理开发环境端口 ($DEV_PORT) ===${NC}"
stop_port $DEV_PORT
echo ""
# 停止生产环境端口
echo -e "${BLUE}=== 方法3: 清理生产环境端口 ($PROD_PORT) ===${NC}"
stop_port $PROD_PORT
echo ""
# 清理所有相关进程
echo -e "${BLUE}=== 方法4: 清理所有相关进程 ===${NC}"
sudo pkill -f "main.go" 2>/dev/null && echo -e "${GREEN}✅ 已清理所有 main.go 进程${NC}" || echo -e "${YELLOW}未找到其他相关进程${NC}"
# 等待进程完全退出
sleep 2
echo ""
# 验证
echo -e "${BLUE}=== 验证结果 ===${NC}"
# 检查端口
for PORT in $DEV_PORT $PROD_PORT; do
if lsof -ti:$PORT > /dev/null 2>&1; then
echo -e "${RED}⚠️ 端口 $PORT 仍被占用:${NC}"
lsof -i:$PORT
else
echo -e "${GREEN}✅ 端口 $PORT 已释放${NC}"
fi
done
# 检查进程
if ps aux | grep "go run main.go" | grep -v grep > /dev/null; then
echo -e "${RED}⚠️ 仍有 go run 进程在运行:${NC}"
ps aux | grep "go run main.go" | grep -v grep
else
echo -e "${GREEN}✅ 所有 go run 进程已停止${NC}"
fi
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} ✅ 服务已停止${NC}"
echo -e "${GREEN}========================================${NC}"