31 lines
564 B
Go
31 lines
564 B
Go
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)
|
||
|
|
}
|