Skip to main content

Process

Struct Process 

Source
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: u32

Process ID

§name: String

Process 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: f32

CPU usage percentage (0.0 - 100.0+)

§memory_mb: f64

Memory usage in megabytes

§status: ProcessStatus

Process 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

Source

pub fn names(&self) -> Names<'_>

The names a name target is compared with

Source

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.

Source

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.

Source

pub fn find_by_pid(pid: u32) -> Result<Option<Process>>

Find a specific process by PID

Source

pub fn find_all() -> Result<Vec<Process>>

Get all running processes (excludes proc’s own process)

Source

pub fn find_by_exe_path(path: &Path) -> Result<Vec<Process>>

Find processes running a specific executable path

Source

pub fn find_by_open_file(path: &Path) -> Result<Vec<Process>>

Find processes that have a file open (Unix only via lsof)

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn kill(&self) -> Result<()>

Force kill the process (SIGKILL on Unix, taskkill /F on Windows)

Source

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.

Source

pub fn terminate(&self) -> Result<()>

Send SIGTERM for graceful termination (Unix) or taskkill (Windows)

Source

pub fn send_signal(&self, signal: Signal) -> Result<()>

Send an arbitrary signal to the process (Unix only)

Source

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.

Source

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.

Source

pub fn is_running(&self) -> bool

Check if the process is still running (alias for exists for compatibility)

Trait Implementations§

Source§

impl Clone for Process

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Process

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Process

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Process

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.