From 67230ee4b8f2bfc65aa0f01177259c17661d72b4 Mon Sep 17 00:00:00 2001 From: naiba Date: Fri, 29 Jan 2021 09:22:24 +0800 Subject: [PATCH] =?UTF-8?q?[dashboard=200.4.1=20&=20agent=200.3.4]=20?= =?UTF-8?q?=F0=9F=9A=B8=20=E6=B7=BB=E5=8A=A0=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=96=87=E6=A1=88=E3=80=81=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- cmd/agent/main.go | 25 +++++++++++++++++++------ resource/template/component/cron.html | 2 +- service/dao/dao.go | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5f0174f..cfca507 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 哪吒面板 -![dashboard](https://img.shields.io/badge/管理面板-v0.4.0-brightgreen?style=for-the-badge&logo=github) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) +![dashboard](https://img.shields.io/badge/管理面板-v0.4.1-brightgreen?style=for-the-badge&logo=github) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) 系统状态监控报警、API(SSL证书变更、即将到期、到期)/TCP端口存活/PING 监控、计划任务(可以定时在Agent上执行命令,备份、重启、What ever you want)、极省资源,64M 服务器也能装 agent。 diff --git a/cmd/agent/main.go b/cmd/agent/main.go index ed2f89c..76515a9 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -232,19 +232,32 @@ func doTask(task *pb.Task) { case model.TaskTypeCommand: startedAt := time.Now() var cmd *exec.Cmd + var resChan chan string + var errChan chan string + timeout := time.NewTimer(time.Minute * 30) if utils.IsWindows() { cmd = exec.Command("cmd", "/c", task.GetData()) } else { cmd = exec.Command("sh", "-c", task.GetData()) } - output, err := cmd.Output() - result.Delay = float32(time.Now().Sub(startedAt).Seconds()) - if err != nil { - result.Data = fmt.Sprintf("%s\n%s", string(output), err.Error()) - } else { - result.Data = string(output) + go func(resChan, errChan chan string) { + output, err := cmd.Output() + if err != nil { + errChan <- fmt.Sprintf("%s\n%s", string(output), err.Error()) + return + } + resChan <- string(output) + }(resChan, errChan) + select { + case <-timeout.C: + result.Data = "任务执行超时(30分钟)" + case output := <-resChan: + result.Data = output result.Successful = true + case errString := <-errChan: + result.Data = errString } + result.Delay = float32(time.Now().Sub(startedAt).Seconds()) default: log.Printf("Unknown action: %v", task) } diff --git a/resource/template/component/cron.html b/resource/template/component/cron.html index 2c235ae..2f15311 100644 --- a/resource/template/component/cron.html +++ b/resource/template/component/cron.html @@ -37,7 +37,7 @@ 计划的格式为:* * * * * 分 时 天 月 星期,详情见 计划表达式格式
- 命令:就像写 shell/bat 脚本一样,但是不推荐换行,多个命令使用 `&&`/`&` 连接,如果遇到 xxx 命令找不到,可能是 PATH 环境变量的问题,`Linux` 主机在命令开头加入 + 命令:就像写 shell/bat 脚本一样,但是不推荐换行,多个命令使用 &&/& 连接,如果遇到 xxx 命令找不到,可能是 PATH 环境变量的问题,Linux 主机在命令开头加入 source ~/.bashrc或者使用绝对路径执行。

diff --git a/service/dao/dao.go b/service/dao/dao.go index 8cd0cdd..fe9127b 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -13,7 +13,7 @@ import ( pb "github.com/naiba/nezha/proto" ) -var Version = "v0.4.0" // !!记得修改 README 重的 badge 版本!! +var Version = "v0.4.1" // !!记得修改 README 重的 badge 版本!! const ( SnapshotDelay = 3