fix: run dpkg command with timeout

This commit is contained in:
2024-01-11 16:55:35 +08:00
parent 9b7f3742d0
commit 5aeca6ecb4

View File

@@ -2,6 +2,7 @@ package cm
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
@@ -9,6 +10,7 @@ import (
"os/exec"
"strconv"
"strings"
"time"
"ems.agt/lib/dborm"
"ems.agt/lib/global"
@@ -699,14 +701,17 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
} else if fileType == 2 {
dpkgCmd := fmt.Sprintf("sudo dpkg -i --force-all '%s'", filePath)
cmd := exec.Command("ssh", sshHost, dpkgCmd)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // 设置60秒超时
defer cancel()
cmd := exec.CommandContext(ctx, "ssh", sshHost, dpkgCmd)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
err := cmd.Start()
if err != nil {
log.Error("Faile to execute dpkg command: %v, err: %s", err, stderr.String())
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// out, err := cmd.CombinedOutput()
// log.Debugf("Exec output: %v", string(out))