-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.go
63 lines (51 loc) · 1.89 KB
/
signal.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
package jasper
import (
"context"
"syscall"
"github.com/tychoish/fun/erc"
)
// Terminate sends a SIGTERM signal to the given process under the given
// context. This does not guarantee that the process will actually die. This
// function does not Wait() on the given process upon sending the signal.
func Terminate(ctx context.Context, p Process) error {
return p.Signal(ctx, syscall.SIGTERM)
}
// Kill sends a SIGKILL signal to the given process under the given context.
// This guarantees that the process will die. This function does not Wait() on
// the given process upon sending the signal.
func Kill(ctx context.Context, p Process) error {
return p.Signal(ctx, syscall.SIGKILL)
}
// TerminateAll sends a SIGTERM signal to each of the given processes under the
// given context. This does not guarantee that each process will actually die.
// This function calls Wait() on each process after sending them SIGTERM
// signals. On Windows, this function sends a SIGKILL instead of SIGTERM. Use
// Terminate() in a loop if you do not wish to potentially hang on Wait().
func TerminateAll(ctx context.Context, procs []Process) error {
catcher := &erc.Collector{}
for _, proc := range procs {
if proc.Running(ctx) {
catcher.Add(Terminate(ctx, proc))
}
}
for _, proc := range procs {
_, _ = proc.Wait(ctx)
}
return catcher.Resolve()
}
// KillAll sends a SIGKILL signal to each of the given processes under the
// given context. This guarantees that each process will actually die. This
// function calls Wait() on each process after sending them SIGKILL signals.
// Use Kill() in a loop if you do not wish to potentially hang on Wait().
func KillAll(ctx context.Context, procs []Process) error {
catcher := &erc.Collector{}
for _, proc := range procs {
if proc.Running(ctx) {
catcher.Add(Kill(ctx, proc))
}
}
for _, proc := range procs {
_, _ = proc.Wait(ctx)
}
return catcher.Resolve()
}