pub struct Process { /* private fields */ }
Expand description
Struct containing information of a process.
§iOS
This information cannot be retrieved on iOS due to sandboxing.
§Apple app store
If you are building a macOS Apple app store, it won’t be able to retrieve this information.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.name());
}
Implementations§
source§impl Process
impl Process
sourcepub fn kill(&self) -> bool
pub 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 Process::kill_with
.
To get the list of the supported signals on this system, use
SUPPORTED_SIGNALS
.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
process.kill();
}
sourcepub fn kill_with(&self, signal: Signal) -> Option<bool>
pub 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 Process::kill
directly.
To get the list of the supported signals on this system, use
SUPPORTED_SIGNALS
.
use sysinfo::{Pid, Signal, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
if process.kill_with(Signal::Kill).is_none() {
println!("This signal isn't supported on this platform");
}
}
sourcepub fn name(&self) -> &str
pub 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 Process::exe
instead (which
can be empty sometimes!).
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.name());
}
sourcepub fn cmd(&self) -> &[String]
pub fn cmd(&self) -> &[String]
Returns the command line.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.cmd());
}
sourcepub fn exe(&self) -> Option<&Path>
pub fn exe(&self) -> Option<&Path>
Returns the path to the process.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.exe());
}
§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.
sourcepub fn pid(&self) -> Pid
pub fn pid(&self) -> Pid
Returns the PID of the process.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.pid());
}
sourcepub fn environ(&self) -> &[String]
pub fn environ(&self) -> &[String]
Returns the environment variables of the process.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.environ());
}
sourcepub fn cwd(&self) -> Option<&Path>
pub fn cwd(&self) -> Option<&Path>
Returns the current working directory.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.cwd());
}
sourcepub fn root(&self) -> Option<&Path>
pub fn root(&self) -> Option<&Path>
Returns the path of the root directory.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.root());
}
sourcepub fn memory(&self) -> u64
pub fn memory(&self) -> u64
Returns the memory usage (in bytes).
This method returns the size of the resident set, that is, the amount of memory that the process allocated and which is currently mapped in physical RAM. It does not include memory that is swapped out, or, in some operating systems, that has been allocated but never used.
Thus, it represents exactly the amount of physical RAM that the process is using at the
present time, but it might not be a good indicator of the total memory that the process will
be using over its lifetime. For that purpose, you can try and use
virtual_memory
.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{} bytes", process.memory());
}
sourcepub fn virtual_memory(&self) -> u64
pub fn virtual_memory(&self) -> u64
Returns the virtual memory usage (in bytes).
This method returns the size of virtual memory, that is, the amount of memory that the process can access, whether it is currently mapped in physical RAM or not. It includes physical RAM, allocated but not used regions, swapped-out regions, and even memory associated with memory-mapped files.
This value has limitations though. Depending on the operating system and type of process, this value might be a good indicator of the total memory that the process will be using over its lifetime. However, for example, in the version 14 of MacOS this value is in the order of the hundreds of gigabytes for every process, and thus not very informative. Moreover, if a process maps into memory a very large file, this value will increase accordingly, even if the process is not actively using the memory.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{} bytes", process.virtual_memory());
}
sourcepub fn parent(&self) -> Option<Pid>
pub fn parent(&self) -> Option<Pid>
Returns the parent PID.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.parent());
}
sourcepub fn status(&self) -> ProcessStatus
pub fn status(&self) -> ProcessStatus
Returns the status of the process.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.status());
}
sourcepub fn start_time(&self) -> u64
pub fn start_time(&self) -> u64
Returns the time where the process was started (in seconds) from epoch.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Started at {} seconds", process.start_time());
}
sourcepub fn run_time(&self) -> u64
pub fn run_time(&self) -> u64
Returns for how much time the process has been running (in seconds).
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Running since {} seconds", process.run_time());
}
sourcepub fn cpu_usage(&self) -> f32
pub fn cpu_usage(&self) -> f32
Returns the total CPU usage (in %). Notice that it might be bigger than 100 if run on a multi-core machine.
If you want a value between 0% and 100%, divide the returned value by the number of CPUs.
⚠️ To start to have accurate CPU usage, a process needs to be refreshed twice because CPU usage computation is based on time diff (process time on a given time period divided by total system time on the same time period).
⚠️ If you want accurate CPU usage number, better leave a bit of time
between two calls of this method (take a look at
MINIMUM_CPU_UPDATE_INTERVAL
for
more information).
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}%", process.cpu_usage());
}
sourcepub fn disk_usage(&self) -> DiskUsage
pub fn disk_usage(&self) -> DiskUsage
Returns number of bytes read and written to disk.
⚠️ On Windows, this method actually returns ALL I/O read and written bytes.
use sysinfo::{Pid, System};
let s = System::new_all();
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,
);
}
sourcepub fn user_id(&self) -> Option<&Uid>
pub 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 Users::get_user_by_id
.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("User id for process 1337: {:?}", process.user_id());
}
sourcepub fn effective_user_id(&self) -> Option<&Uid>
pub fn effective_user_id(&self) -> Option<&Uid>
Returns the user ID of the effective owner 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 Users::get_user_by_id
.
If you run something with sudo
, the real user ID of the launched
process will be the ID of the user you are logged in as but effective
user ID will be 0
(i-e root).
⚠️ It always returns None
on Windows.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("User id for process 1337: {:?}", process.effective_user_id());
}
sourcepub fn group_id(&self) -> Option<Gid>
pub fn group_id(&self) -> Option<Gid>
Returns the process group ID of the process.
⚠️ It always returns None
on Windows.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Group ID for process 1337: {:?}", process.group_id());
}
sourcepub fn effective_group_id(&self) -> Option<Gid>
pub fn effective_group_id(&self) -> Option<Gid>
Returns the effective group ID of the process.
If you run something with sudo
, the real group ID of the launched
process will be the primary group ID you are logged in as but effective
group ID will be 0
(i-e root).
⚠️ It always returns None
on Windows.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("User id for process 1337: {:?}", process.effective_group_id());
}
sourcepub fn wait(&self)
pub fn wait(&self)
Wait for process termination.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Waiting for pid 1337");
process.wait();
println!("Pid 1337 exited");
}
sourcepub fn session_id(&self) -> Option<Pid>
pub 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, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("Session ID for process 1337: {:?}", process.session_id());
}
sourcepub fn tasks(&self) -> Option<&HashSet<Pid>>
pub fn tasks(&self) -> Option<&HashSet<Pid>>
Tasks run by this process. If there are none, returns None
.
⚠️ This method always returns None
on other platforms than Linux.
use sysinfo::{Pid, System};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
if let Some(tasks) = process.tasks() {
println!("Listing tasks for process {:?}", process.pid());
for task_pid in tasks {
if let Some(task) = s.process(*task_pid) {
println!("Task {:?}: {:?}", task.pid(), task.name());
}
}
}
}
sourcepub fn thread_kind(&self) -> Option<ThreadKind>
pub fn thread_kind(&self) -> Option<ThreadKind>
If the process is a thread, it’ll return Some
with the kind of thread it is. Returns
None
otherwise.
⚠️ This method always returns None
on other platforms than Linux.
use sysinfo::System;
let s = System::new_all();
for (_, process) in s.processes() {
if let Some(thread_kind) = process.thread_kind() {
println!("Process {:?} is a {thread_kind:?} thread", process.pid());
}
}