process

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 12 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

Index

Constants

View Source
const DefaultStopGrace = 5 * time.Second

DefaultStopGrace is how long a process is given to exit after SIGTERM before it is force-killed with SIGKILL.

Variables

This section is empty.

Functions

func AdoptPTY

func AdoptPTY(fd uintptr, name string) *os.File

AdoptPTY wraps a PTY-master fd inherited from another process (a daemon re-exec passes them as fds 3, 4, …) as a pollable *os.File, matching how Start prepares its own PTY. Returns nil if the fd is not usable.

func CPUPercent

func CPUPercent(pid int) (float64, error)

CPUPercent returns the CPU usage percentage for the given PID. Returns 0 if the process is not found or measurement fails.

func DetectPort

func DetectPort(pid int) (int, error)

DetectPort returns the lowest TCP port a service is listening on, or 0 when it is not listening yet. See DetectPorts, which it wraps.

func DetectPorts added in v0.4.0

func DetectPorts(pids []int) map[int]int

DetectPorts resolves the listening port of several services at once, keyed by the pid devrun tracks. Services with no listener are absent from the result.

Two things make this harder than asking the OS about one pid:

A service's own process usually holds no socket. Commands are spawned as `sh -c` (see hupShield), and a `yarn dev` or `npm start` leaves the listener in a grandchild, so the whole process tree has to be considered.

And both platforms have a trap that answers about the wrong process rather than failing: lsof ORs its selectors unless given -a, and /proc/<pid>/net/tcp is per network namespace, listing every socket on the host. Either one returns a plausible port belonging to something else entirely.

A service listening on several ports — an app port plus a debugger — has no single right answer; the lowest is returned so the value at least stays stable between polls. Declare the port in config when that guess is wrong.

func MemBytes

func MemBytes(pid int) (int64, error)

MemBytes returns the RSS memory usage in bytes for the given PID.

func ParseLsofListenPorts added in v0.4.0

func ParseLsofListenPorts(out string) map[int][]int

ParseLsofListenPorts reads `lsof -FpnT` field output and returns the ports each pid is listening on. Records are one field per line, tagged by their first byte: p a pid, f a new file, n its name, T a TCP state such as TST=LISTEN.

func ParseProcNetTCPListen added in v0.4.0

func ParseProcNetTCPListen(content string) map[string]int

ParseProcNetTCPListen returns the listening port of every row in a /proc/net/tcp or tcp6 table, keyed by socket inode. Columns are sl, local_address, rem_address, st, ... with inode tenth; local_address is hex ADDRESS:PORT and state 0A is TCP_LISTEN.

func TerminateGroup

func TerminateGroup(pid int, grace time.Duration) (killed bool, err error)

TerminateGroup gracefully stops the process group led by pid: it sends SIGTERM to the group, polls for the leader to exit for up to grace, and only then sends SIGKILL to the group. It never reaps the process — a caller that owns Wait must do that. It reports whether SIGKILL had to be sent.

pid must be a process-group leader (pgid == pid), which holds for every process started by Start. Signals target the negative pid so the leader and all of its descendants receive them; if the group is already gone the bare pid is used as a fallback.

The leader-exit poll uses kill(pid, 0), so if pid is a child of the current process it needs a concurrent reaper (the daemon's watchExit, or an eventual Wait) — an exited-but-unreaped zombie keeps the poll alive until the grace period elapses. Re-adopted processes are reaped by init, so this is a non-issue there.

Types

type Process

type Process struct {
	PTY *os.File
	Cmd *exec.Cmd
	Pid int
}

Process represents a running child process with a PTY.

Cmd is nil for a process adopted across a daemon re-exec (it is not a child of the current process); Pid is always set and is what Stop signals.

func Start

func Start(command, cwd string, env map[string]string) (*Process, error)

Start forks the command with a PTY. CWD defaults to current dir if empty. The caller (daemon supervisor) is responsible for reading PTY output.

func (*Process) Stop

func (p *Process) Stop() error

Stop terminates the process gracefully: SIGTERM first, then SIGKILL only if it is still alive after DefaultStopGrace. Signals go to the whole process group — Start runs each command in its own session via the PTY, so the child is a process-group leader — which also reaches services that spawn their own children (e.g. a dev server behind a shell wrapper).

Stop does NOT call Process.Wait — watchExit in the daemon supervisor is the sole owner of Wait, preventing a double-waitpid deadlock.

Jump to

Keyboard shortcuts

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