mirror of https://github.com/ouqiang/gocron
执行本地命令,超时后kill进程
parent
f99db815de
commit
56144ee1a9
|
@ -15,6 +15,20 @@ func ExecShell(command string, args ...string) (string, error) {
|
||||||
return string(result), err
|
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的随机字符串
|
// 生成长度为length的随机字符串
|
||||||
func RandString(length int64) string {
|
func RandString(length int64) string {
|
||||||
sources := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
sources := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
|
|
|
@ -29,8 +29,10 @@ func Create(ctx *macaron.Context) {
|
||||||
}
|
}
|
||||||
ctx.Data["Title"] = "任务管理"
|
ctx.Data["Title"] = "任务管理"
|
||||||
ctx.Data["Hosts"] = hosts
|
ctx.Data["Hosts"] = hosts
|
||||||
ctx.Data["FirstHostName"] = hosts[0].Name
|
if len(hosts) > 0 {
|
||||||
ctx.Data["FirstHostId"] = hosts[0].Id
|
ctx.Data["FirstHostName"] = hosts[0].Name
|
||||||
|
ctx.Data["FirstHostId"] = hosts[0].Id
|
||||||
|
}
|
||||||
ctx.HTML(200, "task/task_form")
|
ctx.HTML(200, "task/task_form")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,9 @@ import (
|
||||||
"github.com/ouqiang/gocron/modules/logger"
|
"github.com/ouqiang/gocron/modules/logger"
|
||||||
"github.com/ouqiang/gocron/modules/ssh"
|
"github.com/ouqiang/gocron/modules/ssh"
|
||||||
"github.com/jakecoffman/cron"
|
"github.com/jakecoffman/cron"
|
||||||
"github.com/ouqiang/gocron/modules/utils"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"github.com/ouqiang/gocron/modules/utils"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Cron *cron.Cron
|
var Cron *cron.Cron
|
||||||
|
@ -57,16 +58,21 @@ type Handler interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 本地命令
|
||||||
type LocalCommandHandler struct {}
|
type LocalCommandHandler struct {}
|
||||||
|
|
||||||
func (h *LocalCommandHandler) Run(taskModel models.TaskHost) (string, error) {
|
func (h *LocalCommandHandler) Run(taskModel models.TaskHost) (string, error) {
|
||||||
args := strings.Split(taskModel.Command, " ")
|
if taskModel.Command == "" {
|
||||||
|
return "", errors.New("invalid command")
|
||||||
if len(args) > 1 {
|
|
||||||
return utils.ExecShell(args[0], args[1:]...)
|
|
||||||
}
|
}
|
||||||
|
fields := strings.Split(taskModel.Command, " ")
|
||||||
return utils.ExecShell(args[0])
|
var args []string
|
||||||
|
if len(fields) > 1 {
|
||||||
|
args = fields[1:]
|
||||||
|
} else {
|
||||||
|
args = []string{}
|
||||||
|
}
|
||||||
|
return utils.ExecShellWithTimeout(taskModel.Timeout, fields[0], args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP任务
|
// HTTP任务
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
<input type="text" name="db_port" value="3306">
|
<input type="text" name="db_port" value="3306">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="two fields">
|
<div class="three fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>用户名</label>
|
<label>用户名</label>
|
||||||
<input type="text" name="db_username">
|
<input type="text" name="db_username">
|
||||||
|
@ -47,7 +47,7 @@
|
||||||
<input type="text" name="db_password">
|
<input type="text" name="db_password">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="two fields">
|
<div class="three fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>数据库名称</label>
|
<label>数据库名称</label>
|
||||||
<input type="text" name="db_name">
|
<input type="text" name="db_name">
|
||||||
|
@ -57,19 +57,19 @@
|
||||||
<input type="text" name="db_table_prefix" value="cron_">
|
<input type="text" name="db_table_prefix" value="cron_">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="two fields">
|
<div class="three fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>管理员账号</label>
|
<label>管理员账号</label>
|
||||||
<input type="text" name="admin_username">
|
<input type="text" name="admin_username">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="two fields">
|
<div class="three fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>管理员密码</label>
|
<label>管理员密码</label>
|
||||||
<input type="text" name="admin_password">
|
<input type="text" name="admin_password">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="two fields">
|
<div class="three fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>管理员邮箱</label>
|
<label>管理员邮箱</label>
|
||||||
<input type="text" name="admin_email">
|
<input type="text" name="admin_email">
|
||||||
|
|
Loading…
Reference in New Issue