Trait sysinfo::ProcessExt[][src]

pub trait ProcessExt: Debug {
Show methods fn kill(&self, signal: Signal) -> bool;
fn name(&self) -> &str;
fn cmd(&self) -> &[String];
fn exe(&self) -> &Path;
fn pid(&self) -> Pid;
fn environ(&self) -> &[String];
fn cwd(&self) -> &Path;
fn root(&self) -> &Path;
fn memory(&self) -> u64;
fn virtual_memory(&self) -> u64;
fn parent(&self) -> Option<Pid>;
fn status(&self) -> ProcessStatus;
fn start_time(&self) -> u64;
fn cpu_usage(&self) -> f32;
fn disk_usage(&self) -> DiskUsage;
}
Expand description

Contains all the methods of the Process struct.

Required methods

Sends the given signal to the process.

use sysinfo::{ProcessExt, Signal, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    process.kill(Signal::Kill);
}

Returns the name of the process.

⚠️ Important ⚠️

On linux, there are two things to know about processes’ name:

  1. It is limited to 15 characters.
  2. It is not always the exe name.

If you are looking for a specific process, unless you know what you are doing, in most cases it’s better to use ProcessExt::exe instead (which can be empty sometimes!).

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}", process.name());
}

Returns the command line.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{:?}", process.cmd());
}

Returns the path to the process.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}", process.exe().display());
}

Returns the pid of the process.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}", process.pid());
}

Returns the environment of the process.

Always empty on Windows, except for current process.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{:?}", process.environ());
}

Returns the current working directory.

Always empty on Windows.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}", process.cwd().display());
}

Returns the path of the root directory.

Always empty on Windows.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}", process.root().display());
}

Returns the memory usage (in KB).

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{} KB", process.memory());
}

Returns the virtual memory usage (in KB).

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{} KB", process.virtual_memory());
}

Returns the parent pid.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{:?}", process.parent());
}

Returns the status of the processus.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{:?}", process.status());
}

Returns the time of process launch (in seconds).

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("Running since {} seconds", process.start_time());
}

Returns the total CPU usage (in %). Notice that it might be bigger than 100 if run on a multicore machine.

If you want a value between 0% and 100%, divide the returned value by the number of CPU processors.

Warning: If you want accurate CPU usage number, better leave a bit of time between two calls of this method (200 ms for example).

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    println!("{}%", process.cpu_usage());
}

Returns number of bytes read and written to disk.

/!\ On Windows, this method actually returns ALL I/O read and written bytes.

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new();
if let Some(process) = s.process(1337) {
    let disk_usage = process.disk_usage();
    println!("read bytes   : new/total => {}/{}",
        disk_usage.read_bytes,
        disk_usage.total_read_bytes,
    );
    println!("written bytes: new/total => {}/{}",
        disk_usage.written_bytes,
        disk_usage.total_written_bytes,
    );
}

Implementors