Files
planet/restart.sh
2026-03-05 11:46:58 +08:00

135 lines
3.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Planet 重启脚本 - 停止并重启所有服务
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}🔄 重启智能星球计划...${NC}"
echo ""
# 停止服务
echo -e "${YELLOW}🛑 停止服务...${NC}"
# 停止后端
if pgrep -f "uvicorn.*app.main:app" > /dev/null 2>&1; then
pkill -f "uvicorn.*app.main:app" 2>/dev/null || true
echo " ✅ 后端已停止"
else
echo " 后端未运行"
fi
# 停止前端
if pgrep -f "vite" > /dev/null 2>&1; then
pkill -f "vite" 2>/dev/null || true
echo " ✅ 前端已停止"
else
echo " 前端未运行"
fi
# 停止 Docker 服务
if command -v docker &> /dev/null && docker ps &> /dev/null 2>&1; then
if command -v docker-compose &> /dev/null; then
docker-compose down > /dev/null 2>&1 || true
else
docker compose down > /dev/null 2>&1 || true
fi
echo " ✅ Docker 容器已停止"
fi
# 等待服务完全停止
echo ""
echo -e "${YELLOW}⏳ 等待进程退出...${NC}"
sleep 2
# 检查端口是否已释放
if lsof -i :8000 > /dev/null 2>&1 || netstat -tlnp 2>/dev/null | grep -q ":8000" || ss -tlnp 2>/dev/null | grep -q ":8000"; then
echo -e "${YELLOW}⚠️ 端口 8000 仍被占用,强制杀死...${NC}"
fuser -k 8000/tcp 2>/dev/null || true
sleep 1
fi
if lsof -i :3000 > /dev/null 2>&1 || netstat -tlnp 2>/dev/null | grep -q ":3000" || ss -tlnp 2>/dev/null | grep -q ":3000"; then
echo -e "${YELLOW}⚠️ 端口 3000 仍被占用,强制杀死...${NC}"
fuser -k 3000/tcp 2>/dev/null || true
sleep 1
fi
echo ""
echo -e "${GREEN}✅ 服务已停止${NC}"
echo ""
# 启动服务
echo -e "${BLUE}🚀 启动服务...${NC}"
# 启动 Docker 服务
if command -v docker &> /dev/null && docker ps &> /dev/null 2>&1; then
echo -e "${BLUE}🗄️ 启动数据库...${NC}"
if command -v docker-compose &> /dev/null; then
docker-compose up -d postgres redis > /dev/null 2>&1 || true
else
docker compose up -d postgres redis > /dev/null 2>&1 || true
fi
echo -e "${YELLOW}⏳ 等待数据库就绪...${NC}"
sleep 5
fi
# 同步 Python 依赖
echo -e "${BLUE}🐍 同步 Python 依赖...${NC}"
cd "$SCRIPT_DIR/backend"
uv sync > /dev/null 2>&1 || true
cd "$SCRIPT_DIR"
# 初始化数据库
echo -e "${BLUE}🗃️ 初始化数据库...${NC}"
PYTHONPATH="$SCRIPT_DIR/backend" python "$SCRIPT_DIR/backend/scripts/init_admin.py" > /dev/null 2>&1 || true
# 启动后端
echo -e "${BLUE}🔧 启动后端服务...${NC}"
export PYTHONPATH="$SCRIPT_DIR/backend"
cd "$SCRIPT_DIR/backend"
nohup python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 > /tmp/planet_backend.log 2>&1 &
BACKEND_PID=$!
cd "$SCRIPT_DIR"
echo -e "${YELLOW}⏳ 等待后端启动...${NC}"
sleep 3
if curl -s http://localhost:8000/health > /dev/null 2>&1; then
echo -e "${GREEN}✅ 后端已启动${NC}"
else
echo -e "${RED}❌ 后端启动失败${NC}"
tail -10 /tmp/planet_backend.log
fi
# 启动前端
echo -e "${BLUE}🌐 启动前端服务...${NC}"
cd "$SCRIPT_DIR/frontend"
npm run dev > /tmp/planet_frontend.log 2>&1 &
FRONTEND_PID=$!
cd "$SCRIPT_DIR"
sleep 2
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}✅ 重启完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${BLUE}🌐 访问地址:${NC}"
echo " 后端: http://localhost:8000"
echo " API文档: http://localhost:8000/docs"
echo " 前端: http://localhost:3000"
echo ""
echo -e "${BLUE}📝 日志:${NC}"
echo " tail -f /tmp/planet_backend.log"
echo " tail -f /tmp/planet_frontend.log"