当任务超时时间设置为0时,不限制任务执行时间

pull/21/merge
ouqiang 2017-04-21 13:50:46 +08:00
parent 4978778579
commit b9bf3f3fa8
2 changed files with 16 additions and 7 deletions

View File

@ -88,6 +88,13 @@ func Exec(sshConfig SSHConfig, cmd string) (output string, err error) {
}
defer session.Close()
if sshConfig.ExecTimeout <= 0 {
outputByte, execErr := session.CombinedOutput(cmd)
output = string(outputByte)
err = execErr
return
}
var resultChan chan Result = make(chan Result)
var timeoutChan chan bool = make(chan bool)
go func() {

View File

@ -10,19 +10,21 @@ import (
"github.com/Tang-RoseChild/mahonia"
)
// 执行shell命令
func ExecShell(command string, args ...string) (string, error) {
result, err := exec.Command(command, args...).CombinedOutput()
return string(result), err
}
// 执行shell命令可设置执行超时时间
func ExecShellWithTimeout(timeout int, command string, args... string) (string, error) {
cmd := exec.Command(command, args...)
// 不限制超时时间
if timeout <= 0 {
output ,err := cmd.CombinedOutput()
return string(output), err
}
d := time.Duration(timeout) * time.Second
timer := time.AfterFunc(d, func() {
// 执行超时kill进程
// 超时kill进程
cmd.Process.Kill()
})
output ,err := cmd.CombinedOutput()