//go:build unix package thin import ( "errors" "os/exec" "syscall" ) // ownGroup starts cmd in a process group of its own and makes the end of // its context kill that whole group, not only cmd: a grandchild that // ignores SIGTERM and holds cmd's output dies with it. func ownGroup(cmd *exec.Cmd) { cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Cancel = func() error { return killGroup(cmd) } } // killGroup sends SIGKILL to cmd's process group (-pgid); a group that // is already gone is not an error. func killGroup(cmd *exec.Cmd) error { if cmd.Process == nil { return nil } if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) { return err } return nil }