fix: 修复 sudo 在非交互模式下无法执行的问题

问题原因:
- sudo 命令没有 -n 选项
- 在后台服务中,sudo 会尝试从终端读取密码
- 由于没有终端,命令静默失败

修复内容:
- 添加 sudo -n 选项强制非交互模式
- 如果需要密码会立即失败并返回错误,而不是挂起
This commit is contained in:
shaw
2025-12-18 19:35:12 +08:00
parent bb7bfb6980
commit e9ec2280ec

View File

@@ -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)
}