pub struct Process {Show 14 fields
pub pid: u32,
pub name: String,
pub exe_path: Option<String>,
pub cwd: Option<String>,
pub command: Option<String>,
pub argv0: Option<String>,
pub raw: Option<Box<RawText>>,
pub cpu_percent: f32,
pub memory_mb: f64,
pub status: ProcessStatus,
pub user: Option<String>,
pub parent_pid: Option<u32>,
pub start_time: Option<u64>,
pub uptime_secs: Option<u64>,
}Expand description
Represents a system process with relevant information
Fields§
§pid: u32Process ID
name: StringProcess name (executable name)
exe_path: Option<String>Path to the executable
cwd: Option<String>Current working directory
command: Option<String>Full command line (if available)
argv0: Option<String>The first word of the command line, kept whole (a path with spaces in it stays one piece) and exactly as the OS gave it, for matching names; never printed, and not part of the JSON
raw: Option<Box<RawText>>The name, executable and command line exactly as the OS gave them, when
escaping control characters for display changed any of them. Matching
reads these, so a name matches the same way whether it is looked up in
the process list or against a record; None when nothing was escaped
(the fields above are then the raw text). Not part of the JSON.
cpu_percent: f32CPU usage percentage (0.0 - 100.0+)
memory_mb: f64Memory usage in megabytes
status: ProcessStatusProcess status
user: Option<String>User who owns the process
parent_pid: Option<u32>Parent process ID
start_time: Option<u64>Process start time (Unix timestamp)
uptime_secs: Option<u64>Seconds the process has been running
Implementations§
Source§impl Process
impl Process
Sourcepub fn raw_cwd(&self) -> Option<&str>
pub fn raw_cwd(&self) -> Option<&str>
The working directory exactly as the OS gave it, for matching --in.
cwd is what is shown, with control characters escaped, so a directory
named a<newline>b and one named a\nb (backslash, n) both read
a\nb there; this one tells them apart.
Sourcepub fn find_by_name(pattern: &str, mode: NameMatch) -> Result<Vec<Process>>
pub fn find_by_name(pattern: &str, mode: NameMatch) -> Result<Vec<Process>>
Find all processes a name matches (see NameMatch): by default the
whole name, with --cmd any part of the name or command line.
Proc itself and its ancestor shells never match. When nothing matches by name but processes have the text in their command line, the error says so: it costs nothing, the same pass over the processes counts them.
Sourcepub fn find_all() -> Result<Vec<Process>>
pub fn find_all() -> Result<Vec<Process>>
Get all running processes (excludes proc’s own process)
Sourcepub fn find_by_exe_path(path: &Path) -> Result<Vec<Process>>
pub fn find_by_exe_path(path: &Path) -> Result<Vec<Process>>
Find processes running a specific executable path
Sourcepub fn find_by_open_file(path: &Path) -> Result<Vec<Process>>
pub fn find_by_open_file(path: &Path) -> Result<Vec<Process>>
Find processes that have a file open (Unix only via lsof)
Sourcepub fn find_stuck(timeout: Duration) -> Result<Vec<Process>>
pub fn find_stuck(timeout: Duration) -> Result<Vec<Process>>
Find processes that look stuck: running longer than timeout, and in each
of two readings a second apart either using over half a core or blocked in
uninterruptible sleep. One reading
used to be enough, so a momentary spike counted. A long build at full CPU
still looks the same as a loop that never ends; only the user can tell.
Sourcepub fn looks_stuck_now(&self) -> bool
pub fn looks_stuck_now(&self) -> bool
Whether this process looks stuck now, measured the way find_stuck does
(stuck in two readings a second apart), for a process named as a target.
Sourcepub fn cpu_over(&self, window: Duration) -> Option<f32>
pub fn cpu_over(&self, window: Duration) -> Option<f32>
CPU use over window (at least sysinfo’s minimum), in percent of one core;
None if the process is gone. A reading over a few milliseconds is ~0 for
any process, so a real window is needed.
Unix only (the method does not exist on Windows): its one caller is the gentle recovery unstick tries there;
on Windows unstick only terminates.
Sourcepub fn measure_cpu(processes: &mut [Process])
pub fn measure_cpu(processes: &mut [Process])
Measure the CPU use of these processes over 0.2 s and set cpu_percent,
in percent of one core. A lookup reads CPU a few milliseconds after its
first refresh, which is ~0% even for a process using a whole core, so
commands call this before they show, filter or sort by CPU. One 0.2 s wait
covers the whole batch; commands that never show CPU skip it.
CPU is the CPU time each process used between two reads, over the wall
time we measure between them. sysinfo’s own cpu_usage divides by its
system-wide tick interval instead, which read about 2% for a process
spinning at 100% in some runs.
Sourcepub fn kill(&self) -> Result<()>
pub fn kill(&self) -> Result<()>
Force kill the process (SIGKILL on Unix, taskkill /F on Windows)
Sourcepub fn kill_and_wait(&self) -> Result<()>
pub fn kill_and_wait(&self) -> Result<()>
Force kill and wait up to 5 seconds for the process to be gone.
Never blocks forever (sysinfo’s own kill_and_wait polls with no limit and never ends for a zombie). A process still alive after 5 seconds, such as one stuck in the kernel, is an error.
Sourcepub fn terminate(&self) -> Result<()>
pub fn terminate(&self) -> Result<()>
Send SIGTERM for graceful termination (Unix) or taskkill (Windows)
Sourcepub fn send_signal(&self, signal: Signal) -> Result<()>
pub fn send_signal(&self, signal: Signal) -> Result<()>
Send an arbitrary signal to the process (Unix only)
Sourcepub fn find_orphans() -> Result<Vec<Process>>
pub fn find_orphans() -> Result<Vec<Process>>
Find orphaned processes (parent is PID 1 / init / launchd, excluding system daemons)
Unix only: on Windows this returns ProcError::NotSupported, because
Windows has no PID 1 and does not re-parent a process whose parent exited,
so an empty list would claim “no orphans” where nothing could be checked.
Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Check if the process is still alive. A zombie (exited, not yet reaped by its parent) has exited, and a process with a different start time is a new one that reused the PID, so neither counts.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Check if the process is still running (alias for exists for compatibility)