Is there a way to get the PIDs of all running instances of an executable file on Linux?

  • pidof <programname> – Ipor Sircer Nov 6 '18 at 3:03
  • Ipor Sircer, can you post this as an answer so I can accept? – Agnel Kurian Nov 6 '18 at 3:28

There are a several ways to do this; you can list all processes and find ones that match the name of the executable you're interested in; or, you can list all running processes that have opened that specific executable file.

For example:

# list all processes, grep by filename
$ ps -ef | grep firefox
...(lots of output, need to cut/filter)...

# more specific, e.g., maybe there's also /opt/bin/firefox
$ ps -ef | grep /usr/lib/firefox/firefox

(Note: in the above example, you need to add grep -v grep if scripting this. Just adding this example for basic exploratory analysis.)

Example of built-in that does the same thing (basically):

$ pidof firefox
5915 5892 5851 5809 5753 5509

# more specific
$ pidof /usr/lib/firefox/firefox
5915 5892 5851 5809 5753 5509

This can also inadvertently list different executables with the same name, so some caution required. But it does have a really nice, terse output great for scripting.

Finally, a pretty robust option is to list open files (lsof {program}), and find the corresponding processes that are opening them (an executable file {program} is "open for reading" while it's running):

# note: sudo not always necessary, but might be
$ lsof /usr/lib/firefox/firefox

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.