From e9ec2280ecd7ad8200c7e53476a2e5841b2e5337 Mon Sep 17 00:00:00 2001 From: shaw Date: Thu, 18 Dec 2025 19:35:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20sudo=20=E5=9C=A8?= =?UTF-8?q?=E9=9D=9E=E4=BA=A4=E4=BA=92=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: - sudo 命令没有 -n 选项 - 在后台服务中,sudo 会尝试从终端读取密码 - 由于没有终端,命令静默失败 修复内容: - 添加 sudo -n 选项强制非交互模式 - 如果需要密码会立即失败并返回错误,而不是挂起 --- backend/internal/pkg/sysutil/restart.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/internal/pkg/sysutil/restart.go b/backend/internal/pkg/sysutil/restart.go index aadf4aa7..4ccca9d7 100644 --- a/backend/internal/pkg/sysutil/restart.go +++ b/backend/internal/pkg/sysutil/restart.go @@ -32,7 +32,8 @@ func RestartService() error { // The sub2api user has NOPASSWD sudo access for systemctl commands // (configured by install.sh in /etc/sudoers.d/sub2api). - cmd := exec.Command("sudo", "systemctl", "restart", serviceName) + // Use -n (non-interactive) to prevent sudo from waiting for password input + cmd := exec.Command("sudo", "-n", "systemctl", "restart", serviceName) if err := cmd.Start(); err != nil { return fmt.Errorf("failed to initiate service restart: %w", err) }