-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproc_unix.go
More file actions
42 lines (35 loc) · 1.04 KB
/
proc_unix.go
File metadata and controls
42 lines (35 loc) · 1.04 KB
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
//go:build !windows
package main
import (
"os"
"os/exec"
"syscall"
)
// setProcessGroup puts the command in its own process group.
func setProcessGroup(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
// killProcessGroup sends SIGKILL to the process group.
func killProcessGroup(cmd *exec.Cmd) {
if cmd.Process != nil {
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}
// terminateProcessGroup sends SIGTERM to the process group, asking it to exit
// gracefully. Returns to be escalated via killProcessGroup if it doesn't.
func terminateProcessGroup(cmd *exec.Cmd) {
if cmd.Process != nil {
syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
}
}
// processAlive reports whether any process in the group is still running.
func processAlive(cmd *exec.Cmd) bool {
if cmd == nil || cmd.Process == nil {
return false
}
return syscall.Kill(-cmd.Process.Pid, 0) == nil
}
// termSignals returns the signals to watch for graceful shutdown.
func termSignals() []os.Signal {
return []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}