#!/usr/bin/env bash # HEL-913 运行包 · 共享 CosyVoice TTS 9880 窗口内临时恢复/停回模板(脱敏待批件) # # 只在获批语音窗口内使用;窗口结束必须 stop 停回(force_end.template.sh 会调 # 本脚本 stop)。只按自己写的 pid 句柄停止,快照先行;9880 已被别人启动时 # 不碰。改编自 HEL-910 已实测 tts_ctl.sh。子命令只认 start|stop|status|snapshot。 # # 用法: [ENV=..] bash tts_ctl.template.sh start|stop|status|snapshot # 环境变量: HEL913_BASE / HEL913_TTS_VENV(/opt/voice-agent/cosyvoice/venv/bin/python) set -u case "${1:-}" in start|stop|status|snapshot) ;; *) echo "invalid subcommand" >&2; exit 2 ;; esac BASE=${HEL913_BASE:-/opt/cybergril-hel913} CC=${HEL913_TTS_HOME:-/opt/voice-agent/cosyvoice} TTSVENV=${HEL913_TTS_VENV:-$CC/venv/bin/python} PIDF=$BASE/logs/tts9880.pid SNAP=$BASE/logs/tts9880-before.txt usage() { echo "usage: [ENV=..] bash $0 start|stop|status|snapshot" >&2; exit 2; } ours() { local p=$1 cmd cw case "$p" in ''|*[!0-9]*) return 1 ;; esac [ "$p" != "$$" ] || return 1 kill -0 "$p" 2>/dev/null || return 1 cmd=$(tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null) || return 1 cw=$(readlink "/proc/$p/cwd" 2>/dev/null) || return 1 case "$cmd" in *"server.py"*) ;; *) return 1 ;; esac [ "$cw" = "$CC" ] || return 1 return 0 } pidfile_pid() { [ -f "$PIDF" ] && cat "$PIDF" 2>/dev/null; } case "${1:-}" in status) p=$(pidfile_pid) if [ -n "$p" ]; then ours "$p" && echo "running pid $p (ours)" || echo "pid $p not verified as ours"; else echo "not started by us"; fi curl -s --max-time 3 http://127.0.0.1:9880/health && echo " <- health" || echo "health not answering" exit 0 ;; snapshot) mkdir -p "$BASE/logs" { echo "ts=$(date -u +%FT%TZ)" if curl -s --max-time 3 http://127.0.0.1:9880/health >/dev/null 2>&1; then echo "was=up"; else echo "was=down"; fi ss -ltn 2>/dev/null | grep ':9880 ' || echo "port9880=free"; } > "$SNAP" cat "$SNAP"; exit 0 ;; stop) p=$(pidfile_pid) if [ -z "$p" ]; then echo "not ours to stop (no pid file)"; exit 0; fi if kill -0 "$p" 2>/dev/null && ! ours "$p"; then echo "REFUSED: pid $p alive but not this tts (pid reuse?) — not killing; resolve $PIDF manually" >&2 exit 1 fi if ours "$p"; then kill "$p"; sleep 5; ours "$p" && kill -9 "$p"; fi rm -f "$PIDF" echo "tts stopped (pid $p)" exit 0 ;; esac # start(仅 start 子命令落到这里) mkdir -p "$BASE/logs" p=$(pidfile_pid) if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then echo "REFUSED: live pid handle already exists; inspect/stop first" >&2; exit 1 fi if curl -s --max-time 3 http://127.0.0.1:9880/health >/dev/null 2>&1; then echo "9880 already up — NOT ours, not touching"; exit 0 fi if ss -ltn 2>/dev/null | grep -q ':9880 '; then echo "port 9880 occupied by non-healthy process — abort"; exit 1 fi [ -x "$TTSVENV" ] || { echo "REFUSED: tts venv python not executable: $TTSVENV" >&2; exit 1; } cd "$CC" || exit 1 nohup "$TTSVENV" server.py >> "$CC/tts.log" 2>&1 & echo $! > "$PIDF" echo "tts starting (pid $(cat "$PIDF")) — model load takes up to ~120s" for i in $(seq 1 60); do sleep 2 if curl -s --max-time 3 http://127.0.0.1:9880/health 2>/dev/null | grep -q '"ok"[[:space:]]*:[[:space:]]*true'; then echo "OK: tts healthy after ~$((i*2))s" wc -c < "$CC/tts.log" > "$BASE/logs/tts-log-start-byte" # TTS 段数计数的窗口起点 exit 0 fi kill -0 "$(cat "$PIDF")" 2>/dev/null || { echo "FAIL: tts process died"; tail -10 "$CC/tts.log"; exit 1; } done echo "FAIL: tts not healthy in 120s"; tail -10 "$CC/tts.log" p=$(pidfile_pid) if ours "$p"; then kill "$p"; sleep 2; ours "$p" && kill -9 "$p"; fi rm -f "$PIDF" exit 1