From 56144ee1a9aa20e200c5c3d72f2bc4946e73c05b Mon Sep 17 00:00:00 2001 From: ouqiang Date: Mon, 17 Apr 2017 16:13:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=9C=AC=E5=9C=B0=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=EF=BC=8C=E8=B6=85=E6=97=B6=E5=90=8Ekill=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/utils/utils.go | 14 ++++++++++++++ routers/task/task.go | 6 ++++-- service/task.go | 20 +++++++++++++------- templates/install/create.html | 10 +++++----- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 7854fe0..aaa7009 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -15,6 +15,20 @@ func ExecShell(command string, args ...string) (string, error) { return string(result), err } +// 执行shell命令,可设置执行超时时间 +func ExecShellWithTimeout(timeout int, command string, args... string) (string, error) { + cmd := exec.Command(command, args...) + d := time.Duration(timeout) * time.Second + timer := time.AfterFunc(d, func() { + // 执行超时kill进程 + cmd.Process.Kill() + }) + output ,err := cmd.CombinedOutput() + timer.Stop() + + return string(output), err +} + // 生成长度为length的随机字符串 func RandString(length int64) string { sources := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") diff --git a/routers/task/task.go b/routers/task/task.go index 424b453..e3418b3 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -29,8 +29,10 @@ func Create(ctx *macaron.Context) { } ctx.Data["Title"] = "任务管理" ctx.Data["Hosts"] = hosts - ctx.Data["FirstHostName"] = hosts[0].Name - ctx.Data["FirstHostId"] = hosts[0].Id + if len(hosts) > 0 { + ctx.Data["FirstHostName"] = hosts[0].Name + ctx.Data["FirstHostId"] = hosts[0].Id + } ctx.HTML(200, "task/task_form") } diff --git a/service/task.go b/service/task.go index 8b7b61d..2fdf9a1 100644 --- a/service/task.go +++ b/service/task.go @@ -9,8 +9,9 @@ import ( "github.com/ouqiang/gocron/modules/logger" "github.com/ouqiang/gocron/modules/ssh" "github.com/jakecoffman/cron" - "github.com/ouqiang/gocron/modules/utils" "strings" + "github.com/ouqiang/gocron/modules/utils" + "errors" ) var Cron *cron.Cron @@ -57,16 +58,21 @@ type Handler interface { } +// 本地命令 type LocalCommandHandler struct {} func (h *LocalCommandHandler) Run(taskModel models.TaskHost) (string, error) { - args := strings.Split(taskModel.Command, " ") - - if len(args) > 1 { - return utils.ExecShell(args[0], args[1:]...) + if taskModel.Command == "" { + return "", errors.New("invalid command") } - - return utils.ExecShell(args[0]) + fields := strings.Split(taskModel.Command, " ") + var args []string + if len(fields) > 1 { + args = fields[1:] + } else { + args = []string{} + } + return utils.ExecShellWithTimeout(taskModel.Timeout, fields[0], args...) } // HTTP任务 diff --git a/templates/install/create.html b/templates/install/create.html index 150d4f7..1e7e4e0 100644 --- a/templates/install/create.html +++ b/templates/install/create.html @@ -37,7 +37,7 @@ -
+
@@ -47,7 +47,7 @@
-
+
@@ -57,19 +57,19 @@
-
+
-
+
-
+