新增任务分类,杀死进程等

常驻任务和定时任务
常驻任务暂停时杀死进程
This commit is contained in:
郝大全
2017-06-30 15:49:33 +08:00
parent fbd1f9e1a7
commit 4d8e647523
10 changed files with 160 additions and 17 deletions

View File

@@ -48,3 +48,25 @@ func runCmdWithTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
return err, false
}
}
//func stopCmd(cmd *exec.Cmd) (error,bool) {
// done := make(chan error)
// go func() {
// done <- cmd.Wait()
// }()
//
// var err error
// select {
// case <-time.After(timeout):
// beego.Warn(fmt.Sprintf("任务执行时间超过%d秒进程将被强制杀掉: %d", int(timeout/time.Second), cmd.Process.Pid))
// go func() {
// <-done // 读出上面的goroutine数据避免阻塞导致无法退出
// }()
// if err = cmd.Process.Kill(); err != nil {
// beego.Error(fmt.Sprintf("进程无法杀掉: %d, 错误信息: %s", cmd.Process.Pid, err))
// }
// return err, true
// case err = <-done:
// return err, false
// }
//}

View File

@@ -74,7 +74,8 @@ func NewCommandJob(id int, name string, command string) *Job {
job.runFunc = func(timeout time.Duration) (string, string, error, bool) {
bufOut := new(bytes.Buffer)
bufErr := new(bytes.Buffer)
cmd := exec.Command("/bin/bash", "-c", command)
//cmd := exec.Command("/bin/bash", "-c", command)
cmd := exec.Command("sh", "-c", command)
cmd.Stdout = bufOut
cmd.Stderr = bufErr
cmd.Start()
@@ -134,6 +135,10 @@ func (j *Job) Run() {
}
cmdOut, cmdErr, err, isTimeout := j.runFunc(timeout)
if err.Error() == "signal: killed" {
cmdOut += err.Error()
err = nil
}
ut := time.Now().Sub(t) / time.Millisecond