process

package
v0.3.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 10 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 first TCP LISTEN port for the given PID. Returns 0 and no error if the process has no listening port yet.

func MemBytes

func MemBytes(pid int) (int64, error)

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

func ParseLsofPort

func ParseLsofPort(output string) (int, error)

ParseLsofPort parses lsof -i output and returns the first LISTEN port.

func ParseProcNetTCP

func ParseProcNetTCP(content string) (int, error)

ParseProcNetTCP parses /proc/[pid]/net/tcp and returns the first LISTEN port. Lines have local_address in hex: HHHHHHHH:PPPP where PPPP is the port in hex. State 0A = 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