Files
hair/start.sh
T
xslandClaude Opus 4.8 023bb2e6fd feat(worker): start.sh 改为后台服务开关(start/stop/restart/status)
- ./start.sh 现支持 start/stop/restart/status 子命令,无参默认 start
- 后台运行 + PID 文件(worker.pid) + 日志(worker.log),手动控制开关
- 前台热重载开发仍用 run_worker.sh
- gitignore 补 worker.pid / worker.log

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 17:47:49 +08:00

68 lines
1.6 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
# worker 服务手动开关(接口1 四庭七眼测量)。
#
# 用法:
# ./start.sh # = start,后台启动
# ./start.sh start
# ./start.sh stop
# ./start.sh restart
# ./start.sh status
# HOST=127.0.0.1 PORT=8187 ./start.sh # 用环境变量覆盖监听地址
#
# 后台运行:日志写 worker.logPID 写 worker.pid(均已 gitignore)。
# 开发时若想前台+热重载,用 ./run_worker.sh。
set -uo pipefail
cd "$(dirname "$0")"
HOST="${HOST:-0.0.0.0}"
PORT="${PORT:-8187}"
PIDFILE="worker.pid"
LOGFILE="worker.log"
UVICORN="./venv/bin/uvicorn"
is_running() {
[ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null
}
start() {
if is_running; then
echo "已在运行 (PID $(cat "$PIDFILE")),监听 ${HOST}:${PORT}"
return 0
fi
rm -f "$PIDFILE"
if [ ! -x "$UVICORN" ]; then
echo "找不到 $UVICORN,请先创建 venv 并安装依赖" >&2
exit 1
fi
nohup "$UVICORN" app:app --host "$HOST" --port "$PORT" >> "$LOGFILE" 2>&1 &
echo $! > "$PIDFILE"
echo "已启动 (PID $!),监听 ${HOST}:${PORT}"
echo "日志: tail -f $LOGFILE 就绪后 /health 返回 200"
}
stop() {
if is_running; then
kill "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
echo "已停止"
else
echo "未在运行"
rm -f "$PIDFILE"
fi
}
status() {
if is_running; then
echo "运行中 (PID $(cat "$PIDFILE")),监听 ${HOST}:${PORT}"
else
echo "未运行"
fi
}
case "${1:-start}" in
start) start ;;
stop) stop ;;
restart) stop; sleep 1; start ;;
status) status ;;
*) echo "用法: $0 {start|stop|restart|status}"; exit 1 ;;
esac