4
0

executor_unix.go 948 B

123456789101112131415161718192021222324252627282930313233343536
  1. //go:build !windows
  2. package executor
  3. import (
  4. "context"
  5. "os/exec"
  6. "syscall"
  7. )
  8. func (e *Executor) Kill(execReq *InternalLogEntry) error {
  9. // A negative PID means to kill the whole process group. This is *nix specific behavior.
  10. return syscall.Kill(-execReq.Process.Pid, syscall.SIGKILL)
  11. }
  12. func wrapCommandInShell(ctx context.Context, finalParsedCommand string) *exec.Cmd {
  13. cmd := exec.CommandContext(ctx, "sh", "-c", finalParsedCommand)
  14. // This is to ensure that the process group is killed when the parent process is killed.
  15. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  16. return cmd
  17. }
  18. func wrapCommandDirect(ctx context.Context, execArgs []string) *exec.Cmd {
  19. if len(execArgs) == 0 {
  20. return nil
  21. }
  22. cmd := exec.CommandContext(ctx, execArgs[0], execArgs[1:]...)
  23. // This is to ensure that the process group is killed when the parent process is killed.
  24. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  25. return cmd
  26. }