Files
sanguo_vnpy_v2/scripts/nas_sync/vps_restart_resident.sh
T

67 lines
3.0 KiB
Bash
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.
#!/bin/bash
# VPS 常驻进程滚动重启(sanguo-live-supervisor + sanguo-shadow-desk)。
#
# 为什么需要:vps-deploy 只推代码+重启 sanguo-apisupervisor/影子柜台是常驻
# python 进程,不重启就一直跑旧代码(2026-08-15 实录:部署后 16h 旧进程原样跑)。
#
# 用法:
# bash scripts/nas_sync/vps_restart_resident.sh # 直接执行(Mac runner / 本机)
#
# 盘中保护:北京时间 工作日 09:15-15:30 跳过重启(盘中杀树丢撮合现场),
# 打印 SKIP_RESIDENT_RESTART 并 exit 0 —— 代码已就位,下次非盘中部署/手动补跑生效。
set -u
VPS=49.232.102.198
SSH="ssh -o ControlPath=none -o ConnectTimeout=20 $VPS"
# ---- 1) 盘中保护 ----
BJ_HM=$(TZ=Asia/Shanghai date +%H%M)
BJ_DOW=$(TZ=Asia/Shanghai date +%u)
if [ "$BJ_DOW" -le 5 ] && [ "$BJ_HM" -ge 0915 ] && [ "$BJ_HM" -le 1530 ]; then
echo "SKIP_RESIDENT_RESTART 北京时间盘中(周$BJ_DOW $BJ_HM)——supervisor/影子保持运行,下次非盘中部署生效"
exit 0
fi
# ---- 2) 查 supervisor/影子 python 进程树根(动态查 PID:账户号会变)----
PIDS=$($SSH "wmic process where \"name='python.exe' and (commandline like '%sanguo_live%supervisor%' or commandline like '%sanguo_trader.shadow%')\" get processid /format:csv" 2>/dev/null \
| tr -d '\r' | grep -oE '[0-9]+$' | sort -u || true)
echo "重启前进程: $(echo $PIDS | tr '\n' ' ')"
# ---- 3) 杀全树(/T 带走 runner_live / shadow --account 子进程)----
for pid in $PIDS; do
$SSH "taskkill /F /T /PID $pid" 2>/dev/null || echo " (PID $pid 已自行退出)"
done
# schtask 收尸(壳可能残留 running 态,end 掉才好 /run
$SSH "schtasks /end /tn sanguo-live-supervisor" 2>/dev/null || true
$SSH "schtasks /end /tn sanguo-shadow-desk" 2>/dev/null || true
# ---- 4) 重拉 ----
sleep 3
# schtasks /run 带重试(2026-08-26 run756 实锤: 短时海量 ssh 连接可被 sshd 重置
# `kex_exchange_identification: Connection reset`——单发失败=影子舰队整夜静默,
# 需手动 schtasks /run 补拉; 3 次重试吃掉瞬时抖动)
run_task() {
local tn=$1 i
for i in 1 2 3; do
$SSH "schtasks /run /tn $tn" && return 0
echo " (拉起 $tn${i}次失败, 5s 后重试)"
sleep 5
done
return 1
}
run_task sanguo-live-supervisor || { echo "❌ supervisor 拉起失败"; exit 1; }
run_task sanguo-shadow-desk || { echo "❌ shadow-desk 拉起失败"; exit 1; }
# ---- 5) 等进程回来(内存实测 40-60s 起齐,给 120s 余量)----
for i in $(seq 1 12); do
sleep 10
GOT=$($SSH "wmic process where \"name='python.exe' and (commandline like '%sanguo_live%supervisor%' or commandline like '%sanguo_trader.shadow%')\" get processid /format:csv" 2>/dev/null \
| tr -d '\r' | grep -cE '[0-9]+$' || true)
echo " ${i}0s: 已起 $GOT 个进程"
if [ "${GOT:-0}" -ge 2 ]; then
echo "RESIDENT_RESTART_DONE supervisor+影子已重启并吃上新代码"
exit 0
fi
done
echo "❌ 120s 内常驻进程未起齐(期望≥2: supervisor+shadow auto)"
exit 1