Help us shape the future of Go! Take the 2026 Go Developer Survey

proc

package
v0.135.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package proc reads the Linux process table from /proc once and answers the questions beekeeper asks of it: who is whose parent, which session a process belongs to, how long it has run. Reading /proc directly instead of grepping `ps` output means a pattern never matches the command that looks for it.

Index

Constants

View Source
const Niceness = "10"

Niceness is the nice level of a command started under a Background context: behind every interactive process on a starved machine.

Variables

This section is empty.

Functions

func Alive added in v0.6.0

func Alive(pid int) bool

Alive reports whether a process with that PID exists.

func AnonKiB

func AnonKiB(pid int) int

AnonKiB returns the anonymous resident memory of pid in KiB: what killing it would give back.

func Background added in v0.38.1

func Background(ctx context.Context) context.Context

Background marks ctx: the commands Command starts under it run at nice Niceness.

func Cgroup added in v0.5.0

func Cgroup(pid int) string

Cgroup returns the cgroup v2 path of pid ("/user.slice/…"), or "".

func Command added in v0.38.1

func Command(ctx context.Context, name string, arg ...string) *exec.Cmd

Command is exec.CommandContext for a read the caller can do without: under a Background context it runs through nice (when the machine has it), and a command killed by ctx gives up its pipes after waitDelay.

func Cwd

func Cwd(pid int) string

Cwd returns the working directory of pid.

func Environ

func Environ(pid int) (map[string]string, error)

Environ returns the environment of pid (readable for the caller's own processes).

func IsBackground added in v0.38.1

func IsBackground(ctx context.Context) bool

IsBackground reports whether ctx is marked by Background.

func LookPath added in v0.108.1

func LookPath(name string) (string, error)

LookPath is exec.LookPath, then the Go tool directories for a bare name PATH lacks: beside the running binary, $GOBIN, every $GOPATH/bin, ~/go/bin and ~/.go/bin, in that order. A systemd user unit, the sandbox broker for one, runs with the service manager's PATH, the distribution's default without the directory go install writes to, where kind and the other Go CLIs live beside beekeeper. A tool found nowhere fails naming the directories looked in; a name with a path separator is exec's alone.

func Named added in v0.101.0

func Named(name string, uid int) []int

Named returns the PIDs of the live processes whose command name (comm) is name and which run as uid.

func Started added in v0.15.0

func Started(pid int) (time.Time, error)

Started is when the live process pid started.

func UID added in v0.5.0

func UID(pid int) int

UID returns the real user id pid runs as, or -1 when it cannot be read.

func WriteBytes added in v0.100.0

func WriteBytes(pid int) (int64, error)

WriteBytes is what the live process pid caused to be written to storage (write_bytes of /proc/<pid>/io); another user's process is unreadable.

Types

type Process

type Process struct {
	PID   int
	PPID  int
	Comm  string
	Args  []string
	Start time.Time
	// StartTicks is Start as the kernel counts it: clock ticks since boot.
	StartTicks int64
	// CPU is the processor time it has burned (user and system).
	CPU time.Duration
	// RSSKiB is its resident memory, file-backed pages included.
	RSSKiB int
}

Process is one entry of the table.

func (*Process) Cmdline

func (p *Process) Cmdline() string

Cmdline is the argument vector joined by spaces.

func (*Process) Elapsed

func (p *Process) Elapsed(now time.Time) time.Duration

Elapsed is how long the process has run at now.

type Table

type Table struct {
	ByPID map[int]*Process
	// contains filtered or unexported fields
}

Table is a snapshot of the process table.

func Read

func Read() (*Table, error)

Read scans /proc.

func ReadAt added in v0.13.1

func ReadAt(root string) (*Table, error)

ReadAt scans the process tree under root: /proc, or a copy of it (each process's stat, cmdline and environ, and the btime line of stat) captured into testdata.

func (*Table) Ancestors

func (t *Table) Ancestors(pid int) []*Process

Ancestors returns the parent chain of pid, nearest first.

func (*Table) AnonKiB added in v0.13.1

func (t *Table) AnonKiB(pid int) int

AnonKiB returns the anonymous resident memory of pid in the table's process tree.

func (*Table) Children

func (t *Table) Children(pid int) []*Process

Children returns the direct children of pid.

func (*Table) Cwd added in v0.13.1

func (t *Table) Cwd(pid int) string

Cwd returns the working directory of pid in the table's process tree.

func (*Table) Descendants

func (t *Table) Descendants(pid int) []*Process

Descendants returns every process below pid, depth first.

func (*Table) Environ added in v0.13.1

func (t *Table) Environ(pid int) (map[string]string, error)

Environ returns the environment of pid in the table's process tree.

Jump to

Keyboard shortcuts

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