Checking if a Process is Currently running by name in rust

Viewed 1200

How would I check if a process such as "example.exe" is currently running using the rust programming language.

1 Answers

You can use sysinfo crate, or particularly processes_by_name

You can get iterator to processes containing the name using the function

fn processes_by_name<'a>(&'a self, name: &'a str) -> Box<dyn Iterator<Item = &'a Process> + 'a>

You can use it like this

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

let s = System::new_all();
for process in s.processes_by_name("htop") {
    //check here if this is your process
}

UPDATE: New version (0.23.0) also contains processes_by_exact_name

It returns an iterator to processes with the exact given name You can use it like this

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

let s = System::new_all();
for process in s.processes_by_exact_name("htop") {
    //Your code goes here
}
Related