Files
byre/internal/procgroup/procgroup.go
T

31 lines
564 B
Go
Raw Normal View History

2026-10-20 22:49:11 -07:00
package procgroup
import (
"context"
"os/exec"
"syscall"
)
type Cmd struct {
*exec.Cmd
}
func Command(name string, args ...string) *Cmd {
return group(exec.Command(name, args...))
}
func CommandContext(ctx context.Context, name string, args ...string) *Cmd {
cmd := group(exec.CommandContext(ctx, name, args...))
cmd.Cancel = cmd.Kill
return cmd
}
func group(cmd *exec.Cmd) *Cmd {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return &Cmd{Cmd: cmd}
}
func (cmd *Cmd) Kill() error {
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}