0

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

2
  • pidof <programname> Nov 6, 2018 at 3:03
  • Ipor Sircer, can you post this as an answer so I can accept? Nov 6, 2018 at 3:28

1 Answer 1

3

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
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .