Documentation
¶
Index ¶
- Constants
- func AdoptPTY(fd uintptr, name string) *os.File
- func CPUPercent(pid int) (float64, error)
- func DetectPort(pid int) (int, error)
- func MemBytes(pid int) (int64, error)
- func ParseLsofPort(output string) (int, error)
- func ParseProcNetTCP(content string) (int, error)
- func TerminateGroup(pid int, grace time.Duration) (killed bool, err error)
- type Process
Constants ¶
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 ¶
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 ¶
CPUPercent returns the CPU usage percentage for the given PID. Returns 0 if the process is not found or measurement fails.
func DetectPort ¶
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 ParseLsofPort ¶
ParseLsofPort parses lsof -i output and returns the first LISTEN port.
func ParseProcNetTCP ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.