feat: 优化计划任务脚本执行输出 (#2637)

release-1.7
ssongliu 1 year ago committed by wanghe-fit2cloud
parent 604949de18
commit 8244b679fe

@ -127,4 +127,4 @@ ErrUserFindErr: "Failed to find user {{ .name }} {{ .err }}"
ErrFirewall: "No firewalld or ufw service is detected. Please check and try again!"
#cronjob
ErrBashExecute: "Script execution error, please check stderr-related information in the task output text field."
ErrBashExecute: "Script execution error, please check the specific information in the task output text area."

@ -127,4 +127,4 @@ ErrUserFindErr: "用戶 {{ .name }} 查找失敗 {{ .err }}"
ErrFirewall: "當前未檢測到系統 firewalld 或 ufw 服務,請檢查後重試!"
#cronjob
ErrBashExecute: "腳本執行錯誤,請在任務輸出文本區域中查看 stderr 相關信息。"
ErrBashExecute: "腳本執行錯誤,請在任務輸出文本域中查看具體信息。"

@ -127,4 +127,4 @@ ErrUserFindErr: "用户 {{ .name }} 查找失败 {{ .err }}"
ErrFirewall: "当前未检测到系统 firewalld 或 ufw 服务,请检查后重试!"
#cronjob
ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看 stderr 相关信息。"
ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看具体信息。"

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strings"
"time"
@ -88,24 +89,19 @@ func ExecCronjobWithTimeOut(cmdStr string, workdir string, timeout time.Duration
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
output := new(bytes.Buffer)
cmd.Stdout = io.MultiWriter(output, cmd.Stdout)
cmd.Stderr = io.MultiWriter(output, cmd.Stderr)
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", buserr.New(constant.ErrCmdTimeout)
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr:\n%s", stderr.String())
err = buserr.New(constant.ErrBashExecute)
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s \n\n stdout:\n%s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout:\n %s", stdout.String())
}
}
return errMsg, err
return output.String(), err
}
func Execf(cmdStr string, a ...interface{}) (string, error) {

Loading…
Cancel
Save