Files
hair/run_worker.sh
T

45 lines
1.5 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.
#!/usr/bin/env bash
# 启动 GPU worker(接口1 四庭七眼 / 接口2 发际线等)。
# 默认:后台运行,绑定 0.0.0.0:8187,日志写到 worker.log,PID 写到 worker.pid。
# 每次开机手动执行一次即可。停止用 ./stop_worker.sh。
#
# 可用环境变量覆盖:
# HOST=127.0.0.1 PORT=9000 ./run_worker.sh
# FOREGROUND=1 ./run_worker.sh # 前台运行(调试用,Ctrl+C 退出)
set -euo pipefail
cd "$(dirname "$0")"
VENV_UVICORN="./venv/bin/uvicorn"
PID_FILE="worker.pid"
LOG_FILE="worker.log"
HOST="${HOST:-0.0.0.0}"
PORT="${PORT:-8187}"
if [ ! -x "$VENV_UVICORN" ]; then
echo "找不到 $VENV_UVICORN,请先创建 venv 并安装依赖(见 requirements.txt)" >&2
exit 1
fi
# 已在运行则不重复启动
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo "worker 已在运行 (PID $(cat "$PID_FILE")),端口 ${PORT}。如需重启请先 ./stop_worker.sh" >&2
exit 0
fi
if [ "${FOREGROUND:-0}" = "1" ]; then
echo ">> 前台运行 worker: ${HOST}:${PORT}"
exec "$VENV_UVICORN" app:app --host "$HOST" --port "$PORT" --no-proxy-headers
fi
echo ">> 后台启动 worker: ${HOST}:${PORT}"
nohup "$VENV_UVICORN" app:app --host "$HOST" --port "$PORT" --no-proxy-headers >> "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
sleep 1
if kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo ">> 已启动,PID $(cat "$PID_FILE"),日志:$LOG_FILE"
else
echo ">> 启动失败,请查看 $LOG_FILE" >&2
rm -f "$PID_FILE"
exit 1
fi