fix: 修改命令执行函数的超时处理和输出文件名

This commit is contained in:
TsMask
2025-05-29 15:35:52 +08:00
parent 88da71b9d5
commit d9bb31a523
2 changed files with 3 additions and 8 deletions

View File

@@ -8,18 +8,13 @@ import (
"time"
)
// Exec 本地执行命令,默认超时20s 列如:("ls -ls")
// Exec 本地执行命令 列如:("ls -ls")
func Exec(cmdStr string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
cmd := exec.Command("bash", "-c", cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("errCmdTimeout %v", err)
}
if err != nil {
errMsg := ""
if len(stderr.String()) != 0 {