run_web.sh 3.0 KB

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