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

This commit is contained in:
ssongliu 2023-10-23 14:03:19 +08:00 committed by GitHub
parent 8cbbef1ac0
commit e8ece0dfd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 13 deletions

View file

@ -131,4 +131,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."

View file

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

View file

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

View file

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strings"
"time"
@ -81,24 +82,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) {