GetProcessId by name in Rust

Hello everyone, I hope you're all well.

I am brand new on rust, and I have a project to do, the goal being to be able to read and write to a certain memory address.

But before I get there, I would like to know if there was a function that would allow me to get the ProcessId by name.

I saw on the internet that there was std::process::id.

I then wrote a program which is the following:

use std::process;

fn getprocessid(pid: &str) {
    println!("PID is -> {}", process::id(pid.parse().unwrap()));


fn main() {
    println!("Simple test");

But my program doesn't work. Could someone please help me.
Thank you in advance.

std::process::id returns the PID of the current process. To get all processes with a certain name you will need to use platform specific functions. Which OS are you on?


I'm on Linux Mint 20.

On linux you can read for each dir in /proc the file cmdline and get everything before the first nul byte. This is the process name. The directory you read it from represents the PID of the process. For example for PID 1, the file /proc/1/cmdlineis/sbin/init\0` on my system.

You can also call the pidof command, which will return all processes with the given name separated with spaces.

Nb: the process name for Firefox is firefox-bin on Linux.

For an easier interface you might want to look at the sysinfo crate. Particularly get_process. The example prints all processes but you can adapt it to return the pid of the process that matches a name.

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

let s = System::new_all();
for (pid, process) in s.get_processes() {
    println!("{} {}", pid,;

Thanks !

So i written this code :

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

fn main() {
    let s = System::new_all();

    for (pid, process) in s.get_processes() {
        //println!("{} {}", pid,;
        let p1 =;
        match p1 {
            "brave" => {
                println!("Brave Has been founded !");
                println!("PID is -> {}", pid);
            _ => {
                println!("is not launched");



but I've got way too much pid.
when i run : pidof -s brave I only have one pid, for exemple 33308.

How could I have one pid like the one I have thanks to pidof?

By reviewing the documentation you can use get_process_by_name.

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

fn main() {
    let s = System::new_all();

    for process in s.get_process_by_name("ac_client") {
        println!("{} {} ",,;

I tried on another program like assaultcube for example, it returns a single pid directly to me.
43543 ac_client
but when it's a browser like firefox or brave it returns several pid.
2287 brave
2311 brave
10069 brave
2337 brave
13496 brave
2912 brave
22895 brave
2422 brave
23654 brave
44030 brave
2378 brave
39435 brave
2879 brave
2212 brave
2278 brave
2939 brave
2351 brave
2379 brave
2918 brave
13587 brave
33283 brave
2838 brave
2852 brave
2420 brave
2762 brave
39465 brave
13846 brave
13532 brave
2717 brave
2791 brave
24281 brave
2341 brave
2776 brave
2474 brave
I don't understand why.
I'll keep looking for thanks to all of you for your help which was really useful :slight_smile:

1 Like

The -s argument of pidof instruction pidof to only return the pid of a single process, not the pid's of all processes.

Thank you for your explanations. I finally understand.

Now I don't have to know how to read and write in the memory. If I don't find it I'll start a new thread.

Thanks again!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.