fix: 优化启动脚本

This commit is contained in:
Daniel
2026-03-08 00:03:14 +08:00
parent 7db497bc94
commit 321fad8a0d
4 changed files with 64 additions and 3 deletions

26
run.sh
View File

@@ -1,12 +1,14 @@
#!/usr/bin/env bash
# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 启动后端 API5599
# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ → 后台启动后端 API5599
# 若 Nginx 未反代 /api请先设置 API_BASE_URL 再执行,例如:
# export API_BASE_URL="http://你的域名或IP:5599"
# ./run.sh
# 若 Nginx 已将 /api 反代到本机 5599可留空 api.config.jsonapiBase 为空即可)。
# 使用 pm2 保活推荐export USE_PM2=1 && ./run.sh
set -e
cd "$(dirname "$0")"
API_PORT="${PORT:-5599}"
export PORT="$API_PORT"
git fetch origin && git reset --hard origin/master
npm install --no-audit --no-fund
@@ -18,4 +20,22 @@ if [ -n "$API_BASE_URL" ]; then
fi
node build.js
bash start-api.sh
# 后台启动 API优先 pm2否则 nohup
if [ -n "$USE_PM2" ] && command -v pm2 >/dev/null 2>&1; then
echo "[run.sh] 使用 pm2 启动 API端口 ${API_PORT}..."
(cd server && npm install --no-audit --no-fund --silent && pm2 delete view-airtep-api 2>/dev/null; true)
(cd server && pm2 start index.js --name view-airtep-api)
pm2 save 2>/dev/null || true
echo "[run.sh] API 已由 pm2 托管。查看: pm2 list / pm2 logs view-airtep-api"
elif command -v nohup >/dev/null 2>&1; then
echo "[run.sh] 使用 nohup 后台启动 API端口 ${API_PORT}..."
mkdir -p logs
nohup bash start-api.sh >> logs/api.log 2>&1 &
echo "[run.sh] API 已在后台运行。查看日志: tail -f logs/api.log"
else
echo "[run.sh] 前台启动 APICtrl+C 会停止服务)..."
exec bash start-api.sh
fi
echo "[run.sh] 部署完成。前端静态在 dist/API 端口 ${API_PORT}。检查: curl -s http://127.0.0.1:${API_PORT}/api/config"