fix: 修复 sudo 在非交互模式下无法执行的问题
问题原因: - sudo 命令没有 -n 选项 - 在后台服务中,sudo 会尝试从终端读取密码 - 由于没有终端,命令静默失败 修复内容: - 添加 sudo -n 选项强制非交互模式 - 如果需要密码会立即失败并返回错误,而不是挂起
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user