How can I get the process ID of another process by name?
I'm on windows 11. I know that std::process:id
gets the PID of the application.
The get_process_by_name function of the sysinfo crate can be used to get the processes starting with a given name. pid is one of the fields of Process.
use sysinfo::SystemExt;
fn main() {
let mut system = sysinfo::System::new();
system.refresh_all();
for p in system.get_process_by_name("docker") {
println!("{}:{}", p.pid, p.name);
}
}
Using the get_process_list
function is another option.
let ps = system.get_process_list().iter().filter(|(_, p)| p.name.starts_with("docker"));
for (pid, p) in ps {
println!("{}:{}", pid, p.name);
}