Skip to content
Advertisement

How to get PID by process name?

Is there any way I can get the PID by process name in Python?

JavaScript

For example I need to get 3110 by chrome.

Advertisement

Answer

You can get the pid of processes by name using pidof through subprocess.check_output:

JavaScript

check_output(["pidof",name]) will run the command as "pidof process_name", If the return code was non-zero it raises a CalledProcessError.

To handle multiple entries and cast to ints:

JavaScript

In [21]: get_pid(“chrome”)

JavaScript

Or pas the -s flag to get a single pid:

JavaScript
Advertisement