gunk-dev/the-valley

group.go

source47aab6eb837dmain's published tip history
444 bytes
1//go:build unix
2
3package composed
4
5import (
6 "errors"
7 "os"
8 "os/exec"
9 "syscall"
10)
11
12// ownGroup runs cmd in a process group of its own, and has a done context
13// kill the whole group.
14func ownGroup(cmd *exec.Cmd) {
15 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
16 cmd.Cancel = func() error {
17 err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
18 if errors.Is(err, syscall.ESRCH) {
19 return os.ErrProcessDone
20 }
21 return err
22 }
23}