cmd/gitbay-runner/proc_unix.go

4effb29e572777a68b56b21da1b1733fb6219e89
gitbay/cmd/gitbay-runner/proc_unix.go history · blame · raw

29 lines · 737 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	if cmd.SysProcAttr == nil {
17		cmd.SysProcAttr = &syscall.SysProcAttr{}
18	}
19	cmd.SysProcAttr.Setpgid = true
20}
21
22func killTree(cmd *exec.Cmd) {
23	if cmd.Process == nil {
24		return
25	}
26	if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
27		cmd.Process.Kill()
28	}
29}