当任务超时时间设置为0时,不限制任务执行时间
parent
4978778579
commit
b9bf3f3fa8
|
@ -88,6 +88,13 @@ func Exec(sshConfig SSHConfig, cmd string) (output string, err error) {
|
||||||
}
|
}
|
||||||
defer session.Close()
|
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 resultChan chan Result = make(chan Result)
|
||||||
var timeoutChan chan bool = make(chan bool)
|
var timeoutChan chan bool = make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -10,19 +10,21 @@ import (
|
||||||
"github.com/Tang-RoseChild/mahonia"
|
"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命令,可设置执行超时时间
|
// 执行shell命令,可设置执行超时时间
|
||||||
func ExecShellWithTimeout(timeout int, command string, args... string) (string, error) {
|
func ExecShellWithTimeout(timeout int, command string, args... string) (string, error) {
|
||||||
cmd := exec.Command(command, args...)
|
cmd := exec.Command(command, args...)
|
||||||
|
|
||||||
|
// 不限制超时时间
|
||||||
|
if timeout <= 0 {
|
||||||
|
output ,err := cmd.CombinedOutput()
|
||||||
|
return string(output), err
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
d := time.Duration(timeout) * time.Second
|
d := time.Duration(timeout) * time.Second
|
||||||
timer := time.AfterFunc(d, func() {
|
timer := time.AfterFunc(d, func() {
|
||||||
// 执行超时kill进程
|
// 超时kill进程
|
||||||
cmd.Process.Kill()
|
cmd.Process.Kill()
|
||||||
})
|
})
|
||||||
output ,err := cmd.CombinedOutput()
|
output ,err := cmd.CombinedOutput()
|
||||||
|
|
Loading…
Reference in New Issue