Files
ljwx-stock/start_with_pm2.sh
brunoandClaude Sonnet 4.5 caa81eec5e fix: PM2启动脚本优化 - 自动清理旧进程和支持多版本PostgreSQL
改进:
1. 自动清理端口8028的旧后端进程(start_app.sh遗留)
2. 清理旧的前端PID文件
3. 支持检测PostgreSQL@15/14而不仅是@16
4. 添加cleanup_old_processes步骤避免端口冲突

修复问题:
- "Address already in use" 错误
- 多次重启失败

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
2025-12-25 17:50:01 +08:00

157 lines
4.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
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
# ==========================================
# ljwx-stock - PM2 启动脚本(更稳定)
# ==========================================
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_banner() {
echo -e "${CYAN}"
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Claude Stock - PM2 稳定启动模式 ║"
echo "╚════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
print_step() { echo -e "\n${BLUE}==>${NC} ${CYAN}$1${NC}"; }
print_success() { echo -e "${GREEN}✓${NC} $1"; }
print_error() { echo -e "${RED}✗${NC} $1"; }
print_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
print_msg() { echo -e "${GREEN}[INFO]${NC} $1"; }
# 检查 PM2 是否安装
check_pm2() {
if ! command -v pm2 >/dev/null 2>&1; then
print_error "PM2 not installed"
echo ""
echo "请安装 PM2:"
echo " ${CYAN}npm install -g pm2${NC}"
echo ""
exit 1
fi
print_success "PM2 已安装"
}
# 检查并启动数据库
start_databases() {
print_step "Starting databases..."
# PostgreSQL - 检测任意版本
if lsof -i :5432 -sTCP:LISTEN -t >/dev/null 2>&1; then
print_msg "PostgreSQL already running"
else
# 尝试启动已安装的任意PostgreSQL版本
for pg_ver in postgresql@16 postgresql@15 postgresql@14 postgresql; do
if brew services list | grep "$pg_ver" >/dev/null 2>&1; then
brew services start "$pg_ver"
print_success "PostgreSQL started ($pg_ver)"
break
fi
done
fi
# Redis
if lsof -i :6379 -sTCP:LISTEN -t >/dev/null 2>&1; then
print_msg "Redis already running"
else
brew services start redis
print_success "Redis started"
fi
sleep 2
}
# 创建日志目录
setup_logs() {
mkdir -p logs
print_success "Log directory created"
}
# 清理旧进程
cleanup_old_processes() {
print_step "Cleaning up old processes..."
# 清理旧的非PM2后端进程(可能来自start_app.sh)
if lsof -ti :8028 >/dev/null 2>&1; then
print_msg "Killing old backend processes on port 8028"
lsof -ti :8028 | xargs kill -9 2>/dev/null || true
sleep 1
fi
# 清理旧的非PM2前端进程
if [ -f ".frontend.pid" ]; then
print_msg "Killing old frontend process"
kill $(cat .frontend.pid) 2>/dev/null || true
rm -f .frontend.pid
fi
# 停止旧的PM2进程(如果存在)
pm2 delete ljwx-stock-frontend ljwx-stock-backend 2>/dev/null || true
print_success "Old processes cleaned"
}
# 启动应用
start_apps() {
print_step "Starting applications with PM2..."
# 使用 PM2 启动
pm2 start ecosystem.config.js
# 保存 PM2 进程列表
pm2 save
print_success "Applications started"
}
# 显示状态
show_status() {
echo ""
print_step "Application Status:"
pm2 list
echo ""
print_step "Access URLs:"
echo " Frontend: ${CYAN}http://localhost:8448${NC}"
echo " Frontend (远程): ${CYAN}http://192.168.1.83:8448${NC}"
echo " Backend: ${CYAN}http://localhost:8028${NC}"
echo " API Docs: ${CYAN}http://localhost:8028/docs${NC}"
echo ""
print_step "PM2 管理命令:"
echo " 查看日志: ${CYAN}pm2 logs ljwx-stock-frontend${NC}"
echo " 重启服务: ${CYAN}pm2 restart ljwx-stock-frontend${NC}"
echo " 停止服务: ${CYAN}pm2 stop all${NC}"
echo " 删除服务: ${CYAN}pm2 delete all${NC}"
echo " 查看监控: ${CYAN}pm2 monit${NC}"
echo ""
}
# 主流程
main() {
print_banner
check_pm2
start_databases
setup_logs
cleanup_old_processes
start_apps
show_status
print_success "✅ All services started successfully!"
echo ""
print_msg "PM2 will automatically restart services if they crash"
echo ""
}
main