mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-02-08 01:20:07 +08:00
fix: 解决计划任务部分脚本执行没有输出的问题 (#908)
This commit is contained in:
parent
5a8deddc63
commit
8a92913230
@ -520,7 +520,6 @@ func (b *BaseApi) Size(c *gin.Context) {
|
|||||||
// @Success 200 {string} content
|
// @Success 200 {string} content
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /files/loadfile [post]
|
// @Router /files/loadfile [post]
|
||||||
// @x-panel-log {"bodyKeys":["path"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"读取文件 [path]","formatEN":"Read file [path]"}
|
|
||||||
func (b *BaseApi) LoadFromFile(c *gin.Context) {
|
func (b *BaseApi) LoadFromFile(c *gin.Context) {
|
||||||
var req dto.FilePath
|
var req dto.FilePath
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
@ -29,7 +29,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
|
|||||||
if len(cronjob.Script) == 0 {
|
if len(cronjob.Script) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stdout, errExec := cmd.ExecWithTimeOut(cronjob.Script, 5*time.Minute)
|
stdout, errExec := cmd.ExecCronjobWithTimeOut(cronjob.Script, 5*time.Minute)
|
||||||
if errExec != nil {
|
if errExec != nil {
|
||||||
err = errExec
|
err = errExec
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,32 @@ func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
|
|||||||
return stdout.String(), nil
|
return stdout.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExecCronjobWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
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 "", buserr.New(constant.ErrCmdTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
errMsg := ""
|
||||||
|
if len(stderr.String()) != 0 {
|
||||||
|
errMsg = fmt.Sprintf("stderr:\n %s", stderr.String())
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func Execf(cmdStr string, a ...interface{}) (string, error) {
|
func Execf(cmdStr string, a ...interface{}) (string, error) {
|
||||||
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
|
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
|
@ -515,6 +515,7 @@ const onRefresh = async () => {
|
|||||||
records.value = res.data.items;
|
records.value = res.data.items;
|
||||||
hasRecords.value = true;
|
hasRecords.value = true;
|
||||||
currentRecord.value = records.value[0];
|
currentRecord.value = records.value[0];
|
||||||
|
loadRecord(currentRecord.value);
|
||||||
} else {
|
} else {
|
||||||
records.value = [];
|
records.value = [];
|
||||||
hasRecords.value = false;
|
hasRecords.value = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user