cmd/gitbay-runner/proc_unix.go
26 lines · 685 bytes
1//go:build unix
2
3package main
4
5import (
6 "os/exec"
7 "syscall"
8)
9
10// A step runs as its own process group, so ending it ends everything it
11// started. Killing only the shell leaves its children alive and holding
12// the log pipe, and a wait on that pipe lasts as long as the longest
13// child: dash forks a single command rather than exec'ing it, so on a
14// Debian host "sh -c 'sleep 120'" survived its shell by two minutes.
15func ownProcessGroup(cmd *exec.Cmd) {
16 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
17}
18
19func killTree(cmd *exec.Cmd) {
20 if cmd.Process == nil {
21 return
22 }
23 if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
24 cmd.Process.Kill()
25 }
26}