| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #!/usr/bin/env bash
- # wind104 运维网页守护脚本(Linux,FastAPI + uvicorn)
- # 用法: scripts/run_web.sh start|stop|restart|status
- # 可覆盖: PYTHON=/home/deployor/data_env/bin/python CONF=config/config.linux.ini
- # WEB_HOST=0.0.0.0 WEB_PORT=8080 APP_DIR LOG_DIR
- #
- # 网页只做三件事:读 runtime/status.json 看 27 个端口联通情况、读当日原始 CSV 尾部看最新数据、
- # 增删改查 DuckDB 基础属性库。它不监听设备端口,和采集进程不抢资源;
- # 改完点表会顶一下 runtime/points.version,采集进程 5 秒内热重载,不用重启采集。
- #
- # 依赖(采集进程只用标准库,网页需要这两个包):
- # /home/deployor/data_env/bin/pip install fastapi uvicorn
- set -u
- # 仓库根 = scripts/ 的上一级;所有命令都在仓库根执行,模块名才解析得到
- APP_DIR_DEFAULT="$(cd "$(dirname "$0")/.." && pwd)"
- APP_DIR="${APP_DIR:-$APP_DIR_DEFAULT}"
- LOG_DIR="${LOG_DIR:-$APP_DIR/logs}"
- PID_FILE="$LOG_DIR/web.pid"
- OUT_LOG="$LOG_DIR/web.log"
- WEB_HOST="${WEB_HOST:-0.0.0.0}"
- WEB_PORT="${WEB_PORT:-9101}"
- mkdir -p "$LOG_DIR"
- cd "$APP_DIR" || exit 1
- if [ -z "${PYTHON:-}" ]; then
- for cand in /home/deployor/data_env/bin/python "$(command -v python3 || true)" python; do
- [ -x "$cand" ] && { PYTHON="$cand"; break; }
- done
- fi
- if [ -z "${CONF:-}" ]; then
- if [ -f config/config.linux.ini ]; then CONF=config/config.linux.ini; else CONF=config/config.ini; fi
- fi
- web_pid() { cat "$PID_FILE" 2>/dev/null; }
- running() { local p; p="$(web_pid)"; [ -n "$p" ] && kill -0 "$p" 2>/dev/null; }
- start() {
- if running; then
- echo "网页已在运行 pid=$(web_pid) http://${WEB_HOST}:${WEB_PORT}/"
- return 0
- fi
- # status.json 由采集进程写,网页启动时没有也只是全灰,不影响启动
- "$PYTHON" -c "import fastapi, uvicorn" 2>/dev/null \
- || { echo "缺少依赖:$PYTHON -m pip install fastapi uvicorn"; return 1; }
- nohup "$PYTHON" -X utf8 -m webapp.app --config "$CONF" --host "$WEB_HOST" --port "$WEB_PORT" \
- >>"$OUT_LOG" 2>&1 </dev/null &
- echo $! > "$PID_FILE"
- sleep 2
- if running; then
- echo "启动成功 pid=$(web_pid) 地址 http://${WEB_HOST}:${WEB_PORT}/ 配置=$CONF 日志=$OUT_LOG"
- else
- echo "启动失败,看 $OUT_LOG"; tail -20 "$OUT_LOG"; return 1
- fi
- }
- stop() {
- running || { echo "未在运行"; rm -f "$PID_FILE"; return 0; }
- kill -TERM "$(web_pid)" 2>/dev/null
- for _ in 1 2 3 4 5; do kill -0 "$(web_pid)" 2>/dev/null && sleep 1 || break; done
- kill -0 "$(web_pid)" 2>/dev/null && kill -KILL "$(web_pid)" 2>/dev/null
- rm -f "$PID_FILE"
- echo "已停止"
- }
- status() {
- running || { echo "网页未运行"; return 1; }
- local p; p="$(web_pid)"
- echo "运行中 pid=$p 地址 http://${WEB_HOST}:${WEB_PORT}/"
- curl -s -m 3 "http://127.0.0.1:${WEB_PORT}/api/status" >/dev/null \
- && echo "接口自检 200" || echo "接口自检失败(看 $OUT_LOG)"
- }
- case "${1:-status}" in
- start) start ;;
- stop) stop ;;
- restart) stop; start ;;
- status) status ;;
- *) echo "用法: $0 start|stop|restart|status"; exit 2 ;;
- esac
|