prepareCommandForTreeKill (util/exec_tree_unix.go)

tokless · internal/util/exec_tree_unix.go · go · MIT
infra, tag:knn-classified

Unix helper to prepare a command for killing its process tree and to perform the kill.

// Source: https://github.com/HoangP8/tokless | License: MIT | Copyright 2026 HoangP8
// Provided via justclaw.it.com; keep this notice with the code.
//go:build !windows

package util

import (
	"os/exec"
	"syscall"
)

func prepareCommandForTreeKill(c *exec.Cmd) {
	if c.SysProcAttr == nil {
		c.SysProcAttr = &syscall.SysProcAttr{}
	}
	c.SysProcAttr.Setpgid = true
}

func killProcessTree(c *exec.Cmd) {
	if c.Process == nil {
		return
	}
	_ = syscall.Kill(-c.Process.Pid, syscall.SIGKILL)
	_ = c.Process.Kill()
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key