158

If I have multiple copies of the same application on the disk, and only one is running, as I can see with ps, how can I know the absolute path to distinguish it from the others?

1

8 Answers 8

183
% sudo ls -l /proc/PID/exe

eg:

% ps -auxwe | grep 24466
root     24466  0.0  0.0   1476   280 ?        S     2009   0:00 supervise sshd
% sudo ls -l /proc/24466/exe
lrwxrwxrwx 1 root root 0 Feb  1 18:05 /proc/24466/exe -> /package/admin/daemontools-0.76/command/supervise
3
  • 2
    In my system (ubuntu 14.04) you do not have to be superuser to run the ls command.
    – jarno
    Mar 5, 2016 at 8:42
  • 5
    @jarno ls: cannot read symbolic link /proc/28783/exe: Permission denied -- it's not about running the ls command, it's about accessing the process info of a process not belonging to you. On my box, about 97% of all processes listed in /proc are root processes, and the others are distributed over 11 different users.
    – Irfy
    Mar 21, 2016 at 12:53
  • 1
    ls: cannot read symbolic link '/proc/87/exe': No such file or directory lrwxrwxrwx 1 root root 0 Oct 9 07:05 /proc/87/exe
    – CS QGB
    Oct 8, 2022 at 23:06
35

Use:

pwdx $pid

This gives you the current working directory of the pid, not its absolute path.

Usually the which command will tell you which is being invoked from the shell:

#> which vlc
/usr/bin/vlc
5
  • 15
    @Kokizzu No, it doesn't because it doesn't answer the question at all. The which command only tells you which binary will be run if you execute the command now. The question was "which binary is already running there". Imagine for example having a dozen jdks on your computer. If you want to know for a running java process which jdk it's been taken from, which doesn't help you with that. It will only tell you which jdk it will be taken from, if you execute it now. The accepted answer is also the correct one.
    – noamik
    Feb 18, 2016 at 9:05
  • An obvious way this answer is wrong: on my machine I run processes with different JDK versions and some 32bits/64bits. If I want to identify the correct jstack/jmap version for the process the answer above will not work while the accepted answer will. Dec 7, 2016 at 7:36
  • 1
    @Kokizzu This only answers the question, "What is the current working directory of the process $pid?" The edited post still doesn't answer the question. which merely tells "If the command is on the path, then what is it?" Jun 5, 2018 at 11:42
  • pwdx return me the absolute path of the exectuable program of the process depending on pid on redhat x64 6.3.
    – Nick Dong
    Dec 30, 2018 at 6:05
  • This does not answer the question at all. Dec 30, 2020 at 14:53
19

One way is ps -ef

3
  • 6
    didn't work for a specific service, it just provide the relative path
    – Jader Dias
    Feb 1, 2010 at 16:57
  • Helped me identify a process via the command it was started with.
    – jpierson
    Feb 9, 2016 at 18:50
  • this one can be executed on iOS Dec 17, 2020 at 10:28
7
ps auxwwwe

Source:

https://serverfault.com/questions/62322/getting-full-path-of-executables-in-ps-auxwww-output

3
  • 3
    does not show ALL full qualified paths on my linux: "root 24466 0.0 0.0 1476 280 ? S 2009 0:00 supervise sshd " for example
    – akira
    Feb 1, 2010 at 17:04
  • This is more accurate than the other answers... maybe not as useful, but more the right answer. Upvoted.
    – John Hunt
    Aug 30, 2017 at 14:55
  • Can you add sample output and how to interpret it (for a self-contained answer)? Display of all environment variables makes for a lot of noise. E.g., is it the "_" variable that contains the answer? Nov 20, 2020 at 5:27
7

lsof is an option. You can try something like below:

lsof -p PROCESS_ID

This will list all the files opened by the process including the executable's actual location. It is then possible to add a few more awk, cut, grep etc. to find out the information that you are looking for.

As an example, I executed the following commands to identify where my 'java' process came from:

lsof -p 12345 | awk '{print $NF}' | grep 'java$'
1
2

The quick answer is to use ps with options or the /proc filesystem info. That will usually work, but is not guaranteed. In general, there is no definite, guaranteed answer. For instance, what if the executing file is deleted during execution, so that there is no path to the file?

See the Unix FAQ for a little more detail, particularly questions 4.3 and 4.4.

0

You could use

readlink /proc/$(pgrep -x -U $(id -ur) APP_NAME)/exe

or

find /proc/$(pgrep -x -U $(id -ur) APP_NAME)/exe -printf "%l\n"

to get the absolute path of APP_NAME running as current user.

2
  • AFAICT this isn't using the PID at all and just errors out Apr 21, 2020 at 0:03
  • @electrovir fixed the description.
    – jarno
    Apr 21, 2020 at 4:38
0

UPDATED due to David Moles' comment, below.

See other answers for the path to the executable.

In case you need it, this will give you the working directory of the process. You don't need to know the PID:

pwdx `pgrep ###process_name###`
4
  • The question states “as I can see with ps”, so it will probably display the PID
    – Scz
    Apr 9, 2017 at 13:24
  • Ah ok true. I still find this to be a quicker one liner in many of my use cases.
    – moodboom
    Apr 9, 2017 at 15:41
  • This tells you the current working directory of the process, not the path to its executable. Jan 14, 2021 at 20:08
  • Good point, they may be different.
    – moodboom
    Jan 15, 2021 at 15:56

You must log in to answer this question.

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