From c022ac98cd00f098e54e93be105b2009c2ed909d Mon Sep 17 00:00:00 2001 From: JianFeeeee Date: Sun, 27 Sep 2026 19:52:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20deploy-waiter=20=E7=9A=84=20ssh?= =?UTF-8?q?=20=E5=8A=A0=20-n=EF=BC=8C=E5=90=A6=E5=88=99=E3=80=8C=E5=96=82?= =?UTF-8?q?=E4=BA=86=20yes=20=E5=8D=B4=E8=AF=B4=E5=B7=B2=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `do_check` 里的 ssh 会从 stdin 读,把后续 `read -p "确认更新"` 的输入吃掉 —— 于是 `bash deploy-waiter.sh deploy <<< "yes"` 里的 yes 被 ssh 消耗, read 拿到空串,脚本静默走「已取消」分支。 症状极难定位:脚本本身完全正常、备份逻辑没问题,只是"明明喂了 yes"却 什么也没发生。5 处 ssh 统一加 -n。 --- deploy-waiter.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/deploy-waiter.sh b/deploy-waiter.sh index 2f0aacf..397d891 100644 --- a/deploy-waiter.sh +++ b/deploy-waiter.sh @@ -44,7 +44,10 @@ do_check() { t=$(ssh_of "$ip"); [ -z "$t" ] && { info "未知主机: $ip"; return 1; } s=$(sudo_of "$ip") info "──── $ip ────" - ssh -o BatchMode=yes -o ConnectTimeout=8 "$t" " + # ★ -n:ssh 会从 stdin 读,若不截断会**吃掉后面 read 的输入** + # ("yes" 被 ssh 消耗 ⇒ read 拿到空 ⇒ 脚本静默取消)。 + # 症状是"明明喂了 yes 却说已取消",极难定位。 + ssh -n -o BatchMode=yes -o ConnectTimeout=8 "$t" " echo -n ' 主机: '; hostname echo -n ' 当前二进制: '; ls -la $BIN 2>/dev/null | awk '{print \$5\" 字节 \"\$6\" \"\$7\" \"\$8}' echo -n ' 服务: '; systemctl is-active $SVC 2>/dev/null @@ -74,7 +77,7 @@ do_deploy() { scp -q "$NEW" "$t:/tmp/waiter.new" || { info "上传失败"; return 1; } info "已上传" - ssh -o BatchMode=yes -o ConnectTimeout=8 "$t" " + ssh -n -o BatchMode=yes -o ConnectTimeout=8 "$t" " set -e $s cp -a $BIN $BIN.bak-\$(date +%Y%m%d-%H%M%S) echo BACKUP=\$(ls -t $BIN.bak-* | head -1) @@ -89,14 +92,14 @@ do_deploy() { say "更新后验证 $ip" local alive=false for i in 1 2 3 4 5; do - if ssh -o BatchMode=yes -o ConnectTimeout=8 "$t" "systemctl is-active --quiet $SVC" 2>/dev/null; then + if ssh -n -o BatchMode=yes -o ConnectTimeout=8 "$t" "systemctl is-active --quiet $SVC" 2>/dev/null; then alive=true; break fi sleep 3 done if [ "$alive" = true ]; then info "✓ 服务 active" - ssh -o BatchMode=yes -o ConnectTimeout=8 "$t" " + ssh -n -o BatchMode=yes -o ConnectTimeout=8 "$t" " echo -n ' 新二进制: '; ls -la $BIN | awk '{print \$5\" 字节\"}' echo -n ' 进程时长: '; ps -o etime= -p \$(pgrep -f 'waiter --daemon' | head -1) 2>/dev/null echo -n ' 配置未变: '; md5sum $CFG | cut -c1-32 @@ -113,7 +116,7 @@ do_rollback() { local ip=$1 t s t=$(ssh_of "$ip"); s=$(sudo_of "$ip") say "回滚 $ip" - ssh -o BatchMode=yes -o ConnectTimeout=8 "$t" " + ssh -n -o BatchMode=yes -o ConnectTimeout=8 "$t" " set -e BAK=\$(ls -t $BIN.bak-* 2>/dev/null | head -1) [ -n \"\$BAK\" ] || { echo '找不到备份'; exit 1; }