-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_darwin.go
84 lines (73 loc) · 1.62 KB
/
command_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package command
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"syscall"
"time"
)
// ExecCommand 执行cmd命令操作
func execCommand(name string, args []string, opt ...Option) (out string, err error) {
var (
stderr, stdout bytes.Buffer
expire = 30 * time.Minute
errs []string
)
if len(opt) > 0 && opt[0].Timeout != nil {
expire = *opt[0].Timeout
}
cmd := exec.Command(name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if len(opt) > 0 && opt[0].Stdout != nil {
cmd.Stdout = opt[0].Stdout
} else {
cmd.Stdout = &stdout
}
if len(opt) > 0 && opt[0].Stderr != nil {
cmd.Stderr = opt[0].Stderr
} else {
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)
if len(opt) > 0 && opt[0].Callback != nil {
opt[0].Callback(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
}
func closeProcess(pid int) {
fmt.Println("kill pid:", pid)
syscall.Kill(-pid, syscall.SIGKILL)
pidMap.Delete(pid)
}