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 <noreply@anthropic.com>
This commit is contained in:
2025-12-25 17:50:01 +08:00
co-authored by Claude Sonnet 4.5
parent 3906e06eef
commit caa81eec5e
+36 -8
View File
@@ -45,16 +45,22 @@ check_pm2() {
start_databases() {
print_step "Starting databases..."
# PostgreSQL
if brew services list | grep postgresql@16 | grep started >/dev/null 2>&1; then
# PostgreSQL - 检测任意版本
if lsof -i :5432 -sTCP:LISTEN -t >/dev/null 2>&1; then
print_msg "PostgreSQL already running"
else
brew services start postgresql@16
print_success "PostgreSQL started"
# 尝试启动已安装的任意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 brew services list | grep redis | grep started >/dev/null 2>&1; then
if lsof -i :6379 -sTCP:LISTEN -t >/dev/null 2>&1; then
print_msg "Redis already running"
else
brew services start redis
@@ -70,13 +76,34 @@ setup_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 delete ljwx-stock-frontend ljwx-stock-backend 2>/dev/null || true
# 使用 PM2 启动
pm2 start ecosystem.config.js
@@ -116,6 +143,7 @@ main() {
check_pm2
start_databases
setup_logs
cleanup_old_processes
start_apps
show_status