Files
wind_power_cal/run.sh
T
2026-07-14 15:43:18 +08:00

80 lines
2.2 KiB
Bash
Executable File
Raw 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
# =====================================================
# run.sh — 打包并前台运行 wind_power(后端托管前端 SPA
#
# 流程:package.sh 打包 → 从 runtime/wind_power/ 启动 wind_server
# (后端同时提供 /api 与 SPA 静态资源,即“前后端一起跑”)
#
# 开发模式前端热更新:
# ./run.sh # 后端托管生产前端,监听 :8848
# cd frontend/web_app && npm run dev # 前端 dev :5173/api 代理到 :8848
#
# 参考: edge_collector/run_cloud.sh
# =====================================================
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="${ROOT_DIR}/runtime/wind_power"
BIN_NAME="wind_server"
stop_existing_processes() {
local pids=()
mapfile -t pids < <(pgrep -x "${BIN_NAME}" 2>/dev/null || true)
if [ "${#pids[@]}" -eq 0 ]; then
return
fi
echo "[run] 终止旧进程 ${BIN_NAME} PID: ${pids[*]}"
kill "${pids[@]}" 2>/dev/null || true
for _ in {1..20}; do
mapfile -t pids < <(pgrep -x "${BIN_NAME}" 2>/dev/null || true)
if [ "${#pids[@]}" -eq 0 ]; then
echo "[run] 旧进程已退出"
return
fi
sleep 0.2
done
echo "[run] 旧进程未正常退出,强制终止 PID: ${pids[*]}"
kill -9 "${pids[@]}" 2>/dev/null || true
sleep 0.5
}
# 1. 终止旧进程
stop_existing_processes
# 2. 打包(传递额外参数,如 --build-type Debug
echo "[run] 打包..."
bash "${ROOT_DIR}/package.sh" "$@"
# 3. 启动
cd "${APP_DIR}"
mkdir -p logs data
export LD_LIBRARY_PATH="${APP_DIR}/libs${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
echo "[run] 启动 ${BIN_NAME} ..."
./${BIN_NAME} &
SERVER_PID=$!
echo ""
echo "========================================"
echo " wind_power 已启动"
echo " PID: ${SERVER_PID}"
echo " 工作目录: ${APP_DIR}"
echo " 访问: http://localhost:8848 (Ctrl+C 停止)"
echo "========================================"
# 4. 等待退出信号
cleanup() {
echo ""
echo "[run] 正在停止..."
kill "${SERVER_PID}" 2>/dev/null || true
wait "${SERVER_PID}" 2>/dev/null || true
echo "[run] 已停止"
}
trap cleanup SIGINT SIGTERM
wait "${SERVER_PID}" 2>/dev/null || true