fix: OCR 在一键部署时注册 systemd 开机自启

EOF

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-18 00:31:13 +08:00
parent 8cf375d178
commit 2fea777600
7 changed files with 142 additions and 101 deletions
+19 -28
View File
@@ -1,10 +1,22 @@
#!/usr/bin/env bash
# 注册 OCR Worker 为 systemd 服务(后台常驻)
# 注册 OCR Worker 为 systemd 服务(后台常驻 + 开机自启
# 推荐:由 deploy/install.sh / update.sh 自动调用;也可单独执行。
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
INSTALL_DIR="${INSTALL_DIR:-$(cd "${ROOT}/../.." && pwd)}"
PORT="${OCR_PORT:-23567}"
# shellcheck source=../ocr-common.sh
source "$(cd "$(dirname "$0")/.." && pwd)/ocr-common.sh"
# 提供最小 log_*,以便单独运行
if ! declare -F log_info >/dev/null 2>&1; then
log_info() { echo "[INFO] $*"; }
log_warn() { echo "[WARN] $*"; }
log_error() { echo "[ERROR] $*" >&2; }
fi
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "请使用 root 运行: sudo bash install-service.sh"
exit 1
@@ -15,39 +27,18 @@ if [[ ! -d "${ROOT}/.venv" ]]; then
exit 1
fi
chmod +x "${ROOT}/start.sh" "${ROOT}/check.sh" "${ROOT}/install.sh"
chmod +x "${ROOT}/start.sh" "${ROOT}/check.sh" "${ROOT}/install.sh" 2>/dev/null || true
cat > /etc/systemd/system/ocr-worker.service <<EOF
[Unit]
Description=Grade Archive OCR Worker (PaddleOCR)
After=network.target
start_ocr_service
wait_ocr_healthy 20 || true
[Service]
Type=simple
WorkingDirectory=${ROOT}
Environment=OCR_USE_GPU=true
Environment=OCR_PORT=${PORT}
Environment=OCR_HOST=0.0.0.0
ExecStart=${ROOT}/.venv/bin/uvicorn app:app --host 0.0.0.0 --port ${PORT} --log-level info
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ocr-worker
systemctl restart ocr-worker
sleep 2
echo ""
echo "==> 服务状态:"
systemctl status ocr-worker --no-pager -l | head -20
echo ""
echo "==> 健康检查:"
curl -sS "http://127.0.0.1:${PORT}/health" || echo "(尚未就绪,请稍等 30 秒后重试: bash check.sh"
curl -sS "http://127.0.0.1:${PORT}/health" || echo "(尚未就绪,请稍等后: curl http://127.0.0.1:${PORT}/health"
echo ""
echo "局域网地址: http://$(hostname -I 2>/dev/null | awk '{print $1}'):${PORT}"
echo "成绩档案系统设置 OCR 地址填: http://192.168.8.6:${PORT} (按实际 IP 修改)"
echo "管理: systemctl status|restart|stop ocr-worker"
echo "日志: journalctl -u ocr-worker -f"