Find *where* the executable is for a currently running process

2

If I have 2 executables (/path/to/gcc/myapp.exe, and /path/to/intel/myapp.exe) where one is compiled with an intel compiler and one is compiled with standard gcc, running both will show up in top as just myapp. If I run both of those and have processes running as myapp, is there a way I can find out which process is coming from which executable?

We have a bunch of apps installed on our cluster and I am interested, not only in which apps are being used the most, but which versions of the apps are being used. If I see 500 occurences of R being run, I would like to know if 100 of those are from /path/to/R-3.2.1/intel/R and the other 400 are from /path/to/R-3.1.3/gcc/R, etc. How can I do this?

drjrm3

Posted 2015-05-25T19:47:13.297

Reputation: 1 164

Answers

4

You can do this with the /proc filesystem, as noted in the proc(5) manual page:

/proc/[pid]/exe

Under Linux 2.2 and later, this file is a symbolic link containing the actual pathname of the executed command.

Because it is a symbolic link, you can read the target (do an ls -l to see the pathname).

The [pid] of course means a process-ID for the particular executable you are interested in, at runtime.

(It is unlikely that you have a system running a Linux kernel that old, so "always works" should be good enough).

To get a count, you simply need a script that walks through /proc, for each directory which you own (since you are running the programs, you can see their /proc data), and matching the pathnames against the ones you are interested int. The readlink program, for instance, is useful in bash scripts (and since it is part of coreutils, you likely have it on a Linux system).

Thomas Dickey

Posted 2015-05-25T19:47:13.297

Reputation: 6 891

You don't have to execute a pathname; you can inspect it. – Thomas Dickey – 2015-05-25T20:45:09.083

2

To do that, you can launch top, and then press c on your keyboard to toggle the full path view.

Toine42

Posted 2015-05-25T19:47:13.297

Reputation: 444

This only works from some of the processes. I'm assuming ones where the entire path was used in the execution instruction? Is there any way to do this when the app was not given the full path? – drjrm3 – 2015-05-25T20:38:17.430

0

If these processes are executed with different command line (for example using full path) you can press c to show/hide full command line with which the process was executed.

pvc

Posted 2015-05-25T19:47:13.297

Reputation: 309