-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
59 lines (51 loc) · 1.72 KB
/
exec.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
package exec
import (
"context"
"os"
"os/exec"
"time"
)
// Exec represents an command executer
type Exec struct {
Signal os.Signal
KillAfterCancel time.Duration // TODO
}
// CommandContext returns *os/exec.Cmd with Setpgid = true
// When ctx cancelled, `github.com/k1LoW/exec.CommandContext` send signal to process group
func (e *Exec) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
if e.Signal == nil {
e.Signal = defaultSignal
}
cmd := commandContext(ctx, name, arg...)
cmd.Cancel = func() error {
return terminate(cmd, e.Signal)
}
return cmd
}
// LookPath is os/exec.LookPath
func LookPath(file string) (string, error) {
return exec.LookPath(file)
}
// Command returns *os/exec.Cmd with Setpgid = true
func Command(name string, arg ...string) *exec.Cmd {
return command(name, arg...)
}
// CommandContext returns *os/exec.Cmd with Setpgid = true
// When ctx cancelled, `github.com/k1LoW/exec.CommandContext` send signal to process group
func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
e := &Exec{
Signal: os.Kill, // Why os.Kill ? => for get close to the behavior of os/exec.ContextCommand
KillAfterCancel: -1,
}
return e.CommandContext(ctx, name, arg...)
}
// TerminateCommand send signal to cmd.Process.Pid process group ( if runtime.GOOS != 'windows' )
// TerminateCommand send taskkill to cmd.Process.Pid ( if runtime.GOOS == 'windows' )
func TerminateCommand(cmd *exec.Cmd, sig os.Signal) error {
return terminate(cmd, sig)
}
// KillCommand send syscall.SIGKILL to cmd.Process.Pid process group
// KillCommand send taskkill to cmd.Process.Pid ( if runtime.GOOS == 'windows' )
func KillCommand(cmd *exec.Cmd) error {
return killall(cmd)
}