proctree

package
v0.54.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package proctree kills a command's whole process tree: a process group on Unix, a job object (KILL_ON_JOB_CLOSE) on Windows, where the shell starts suspended and is resumed only after joining the job. Call New before Start, Started right after Start (it ends a shell that could not join), Kill to end the tree, Close once the command has exited (it also ends children left running in the background).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limits

type Limits struct {
	// MemoryBytes caps the memory of one process and of the whole tree
	// (Windows: ProcessMemoryLimit and JobMemoryLimit; Unix: RLIMIT_DATA).
	MemoryBytes uint64
	// Processes caps how many processes the tree may have at once (Windows:
	// ActiveProcessLimit; Unix: RLIMIT_NPROC, which counts the whole user).
	Processes int
	// CPUTime caps the CPU time one process may use (Windows:
	// PerProcessUserTimeLimit; Unix: RLIMIT_CPU).
	CPUTime time.Duration
}

Limits caps what a command's process tree may use (tools.shell_limits). The zero value keeps every limit off. Windows enforces all three fields on the tree's job object; Unix sets them with setrlimit before the shell starts (memory and CPU time per process, the process count per user - a limitation of RLIMIT_NPROC the documentation spells out). A field the platform cannot enforce is ignored rather than approximated.

func (Limits) Empty

func (l Limits) Empty() bool

Empty reports that the limits put no cap on anything: the tree is created exactly as it would be without limits.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree is the command's process group: the shell starts a new group, so killing -pid reaches every child it spawned.

func New

func New(cmd *exec.Cmd) *Tree

New prepares cmd (before Start) so its whole process tree can be killed.

func NewLimited

func NewLimited(cmd *exec.Cmd, limits Limits) *Tree

NewLimited is New with a resource cap on the tree: the limits are set with setrlimit in a short sh wrapper before the shell itself starts, so the whole tree inherits them. A limit that cannot be set here (a shell without that ulimit flag) leaves that one limit off instead of failing the command.

func (*Tree) Close

func (p *Tree) Close()

Close kills children the command left running in the background.

func (*Tree) Exceeded

func (p *Tree) Exceeded() bool

Exceeded reports whether a limit ended a process of the tree. Unix has no counter for that (setrlimit makes the allocation or the fork itself fail), so the tool reads the command's own output instead: see ShellLimits.limitHit.

func (*Tree) Kill

func (p *Tree) Kill() error

Kill sends SIGKILL to the whole group.

func (*Tree) Started

func (p *Tree) Started() error

Started is a no-op: the group exists from Start.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL