Trait sysinfo::ProcessExt
source · pub trait ProcessExt: Debug {
Show 21 methods
// Required methods
fn kill_with(&self, signal: Signal) -> Option<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 run_time(&self) -> u64;
fn cpu_usage(&self) -> f32;
fn disk_usage(&self) -> DiskUsage;
fn user_id(&self) -> Option<&Uid>;
fn group_id(&self) -> Option<Gid>;
fn wait(&self);
fn session_id(&self) -> Option<Pid>;
// Provided method
fn kill(&self) -> bool { ... }
}
Expand description
Contains all the methods of the Process
struct.
Required Methods§
sourcefn kill_with(&self, signal: Signal) -> Option<bool>
fn kill_with(&self, signal: Signal) -> Option<bool>
Sends the given signal
to the process. If the signal doesn’t exist on this platform,
it’ll do nothing and will return None
. Otherwise it’ll return if the signal was sent
successfully.
If you just want to kill the process, use ProcessExt::kill
directly.
To get the list of the supported signals on this system, use
SystemExt::SUPPORTED_SIGNALS
.
use sysinfo::{Pid, ProcessExt, Signal, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
if process.kill_with(Signal::Kill).is_none() {
eprintln!("This signal isn't supported on this platform");
}
}
sourcefn name(&self) -> &str
fn name(&self) -> &str
Returns the name of the process.
⚠️ Important ⚠️
On linux, there are two things to know about processes’ name:
- It is limited to 15 characters.
- 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::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.name());
}
sourcefn cmd(&self) -> &[String]
fn cmd(&self) -> &[String]
Returns the command line.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.cmd());
}
sourcefn exe(&self) -> &Path
fn exe(&self) -> &Path
Returns the path to the process.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.exe().display());
}
§Implementation notes
On Linux, this method will return an empty path if there
was an error trying to read /proc/<pid>/exe
. This can
happen, for example, if the permission levels or UID namespaces
between the caller and target processes are different.
It is also the case that cmd[0]
is not usually a correct
replacement for this.
A process may change its cmd[0]
value
freely, making this an untrustworthy source of information.
sourcefn pid(&self) -> Pid
fn pid(&self) -> Pid
Returns the pid of the process.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.pid());
}
sourcefn environ(&self) -> &[String]
fn environ(&self) -> &[String]
Returns the environment variables of the process.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.environ());
}
sourcefn cwd(&self) -> &Path
fn cwd(&self) -> &Path
Returns the current working directory.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.cwd().display());
}
sourcefn root(&self) -> &Path
fn root(&self) -> &Path
Returns the path of the root directory.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.root().display());
}
sourcefn memory(&self) -> u64
fn memory(&self) -> u64
Returns the memory usage (in bytes).
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{} bytes", process.memory());
}
sourcefn virtual_memory(&self) -> u64
fn virtual_memory(&self) -> u64
Returns the virtual memory usage (in bytes).
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{} bytes", process.virtual_memory());
}
sourcefn parent(&self) -> Option<Pid>
fn parent(&self) -> Option<Pid>
Returns the parent pid.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.parent());
}
sourcefn status(&self) -> ProcessStatus
fn status(&self) -> ProcessStatus
Returns the status of the processus.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.status());
}
sourcefn start_time(&self) -> u64
fn start_time(&self) -> u64
Returns the time where the process was started (in seconds) from epoch.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Started at {} seconds", process.start_time());
}
sourcefn run_time(&self) -> u64
fn run_time(&self) -> u64
Returns for how much time the process has been running (in seconds).
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Running since {} seconds", process.run_time());
}
sourcefn cpu_usage(&self) -> f32
fn cpu_usage(&self) -> f32
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 CPUs.
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::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}%", process.cpu_usage());
}
sourcefn disk_usage(&self) -> DiskUsage
fn disk_usage(&self) -> DiskUsage
Returns number of bytes read and written to disk.
⚠️ On Windows and FreeBSD, this method actually returns ALL I/O read and written bytes.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(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,
);
}
sourcefn user_id(&self) -> Option<&Uid>
fn user_id(&self) -> Option<&Uid>
Returns the ID of the owner user of this process or None
if this information couldn’t
be retrieved. If you want to get the User
from it, take a look at
SystemExt::get_user_by_id
.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
eprintln!("User id for process 1337: {:?}", process.user_id());
}
sourcefn group_id(&self) -> Option<Gid>
fn group_id(&self) -> Option<Gid>
Returns the process group ID of the process.
⚠️ It always returns None
on Windows.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
eprintln!("Group ID for process 1337: {:?}", process.group_id());
}
sourcefn wait(&self)
fn wait(&self)
Wait for process termination.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
eprintln!("Waiting for pid 1337");
process.wait();
eprintln!("Pid 1337 exited");
}
sourcefn session_id(&self) -> Option<Pid>
fn session_id(&self) -> Option<Pid>
Returns the session ID for the current process or None
if it couldn’t be retrieved.
⚠️ This information is computed every time this method is called.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
eprintln!("Session ID for process 1337: {:?}", process.session_id());
}
Provided Methods§
sourcefn kill(&self) -> bool
fn kill(&self) -> bool
Sends Signal::Kill
to the process (which is the only signal supported on all supported
platforms by this crate).
If you want to send another signal, take a look at ProcessExt::kill_with
.
To get the list of the supported signals on this system, use
SystemExt::SUPPORTED_SIGNALS
.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new();
if let Some(process) = s.process(Pid::from(1337)) {
process.kill();
}