procgroup

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package procgroup is the one way abcd bounds a command together with every process it starts: the command leads its own process group, and the bound kills that group through the pid the command's own handle holds, never by a pattern and never another process. RunToEOF reads a bounded command's output to EOF before it is waited for.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bound

func Bound(cmd *exec.Cmd, grace time.Duration)

Bound makes cmd, built with exec.CommandContext, lead its own process group, and makes the context's end kill the whole group: a pipeline, a recipe's shell or a background child the command started ends with it rather than outliving the bound. The kill is addressed to the group through the pid cmd's handle holds while the command is not yet reaped, so the group id is still its own. grace bounds the wait for the command's output after it exits or is killed (exec.Cmd.WaitDelay), so a process that left the group and held the output pipe cannot make the bound unbounded.

A group of its own is out of the terminal's foreground group, so an interrupt typed at the terminal does not reach it: a caller that should stop on one ends the context on it (signal.NotifyContext).

func Kill

func Kill(cmd *exec.Cmd) error

Kill sends SIGKILL to the process group cmd leads (Lead), addressed through the pid cmd's handle holds. Call it only after cmd started and before Wait has reaped it, so the group id is still its own. A command that never started has no group: Kill returns os.ErrProcessDone for it, as a Cancel does for a process that is gone.

func KillLeft added in v0.16.2

func KillLeft(cmd *exec.Cmd)

KillLeft sends SIGKILL to what is left of the group a command that has exited led (Lead). A group's id stays its own while any member lives, so this reaches what the command left behind, and an empty group is no one's to reach.

func Lead

func Lead(cmd *exec.Cmd)

Lead makes cmd lead its own process group once started, for a caller that bounds it itself (its own timer and select) and stops it with Kill.

func RunToEOF added in v0.16.0

func RunToEOF(ctx context.Context, cmd *exec.Cmd, stdout, stderr io.Writer) error

RunToEOF runs cmd, built with exec.CommandContext(ctx, …), and copies its standard output to stdout and its standard error to stderr, reading each through a pipe to EOF before it calls Wait. A writer that is an *os.File is handed to the command as it is, a nil one is the null device, one writer given for both streams gets one pipe, as exec gives it one descriptor, and a cmd.Stdin that is not a file is fed through a pipe of its own. That stdin is read to its end, so it must be one whose reads do not block (an in-memory reader): nothing bounds a feed that waits on its source. A writer that fails is not written again, the rest of its stream is drained so the command is not left blocked on a full pipe, and its error is returned when the command otherwise succeeded.

exec copies into any other writer on goroutines Wait waits for, and under a WaitDelay that wait is timed from the moment Wait sees the command exit, even one that exited on its own: on a loaded machine the copy of an answer the command had already written could miss it, and Wait reported exec.ErrWaitDelay over a command that succeeded, its output cut (iss-2610272041031418). Read here, nothing is left for exec to copy or to time, so a command that exits 0 is read whole, however late its output closes.

A process the command started that holds a pipe open therefore holds the read until ctx ends. Then Wait is called at once: the command is stopped through its Cancel (exec kills it by default; Bound kills its group) and Wait closes the pipes, so the holder cannot keep the caller past ctx. The command's WaitDelay is left to bound only a command that does not exit once stopped. Output cut short by ctx is never answered as the command's, nor is a failure Wait reports once ctx has ended, however the read ended: the error then wraps ctx's own, with what Wait said in its text.

Types

This section is empty.

Jump to

Keyboard shortcuts

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