You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package command
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"syscall"
"time"
)
// ExecCommand 执行cmd命令操作
func ExecCommand(name string, args []string, timeout ...time.Duration) (out string, err error) {
var (
stderr, stdout bytes.Buffer
expire = 30 * time.Minute
errs []string
)
if len(timeout) > 0 {
expire = timeout[0]
}
cmd := exec.Command(name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Start()
if err != nil {
err = fmt.Errorf("%s\n%s", err.Error(), stderr.String())
return
}
pid := 0
if cmd.Process != nil && cmd.Process.Pid != 0 {
pid = cmd.Process.Pid
pidMap.Store(pid, pid)
}
defer func() {
if pid != 0 {
pidMap.Delete(pid)
}
}()
time.AfterFunc(expire, func() {
if cmd.Process != nil && cmd.Process.Pid != 0 {
errs = append(errs, fmt.Sprintf("execute timeout: %d min.", int(expire.Minutes())))
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
cmd.Process.Kill()
}
})
err = cmd.Wait()
if err != nil {
errs = append(errs, err.Error(), stderr.String())
}
out = stdout.String()
if len(errs) > 0 {
errs = append(errs, out)
err = errors.New(strings.Join(errs, "\n\r"))
}
return
}
// 当主程序退出时从pidMap中获取所有的pid然后kill掉
func CloseChildProccess() {
pidMap.Range(func(key, value interface{}) bool {
if pid, ok := value.(int); ok {
fmt.Println("kill pid:", pid)
syscall.Kill(-pid, syscall.SIGKILL)
}
return true
})
}