How can I know the absolute path of a running process?

99

39

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?

Jader Dias

Posted 2010-02-01T13:48:15.000

Reputation: 13 660

Answers

117

% 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

akira

Posted 2010-02-01T13:48:15.000

Reputation: 52 754

1In my system (ubuntu 14.04) you do not have to be superuser to run the ls command. – jarno – 2016-03-05T08:42:30.913

3@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 – 2016-03-21T12:53:13.833

21

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

seenu

Posted 2010-02-01T13:48:15.000

Reputation: 237

3this answer need more upvote.. – Kokizzu – 2015-01-30T03:02:24.617

8@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 – 2016-02-18T09:05:37.000

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. – Daniel Da Cunha – 2016-12-07T07:36:01.117

@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?" – John Strood – 2018-06-05T11:42:23.937

pwdx return me the absolute path of the exectuable program of the process depending on pid on redhat x64 6.3. – Nick Dong – 2018-12-30T06:05:47.893

14

One way is ps -ef

fpmurphy

Posted 2010-02-01T13:48:15.000

Reputation: 1 260

6didn't work for a specific service, it just provide the relative path – Jader Dias – 2010-02-01T16:57:24.127

Helped me identify a process via the command it was started with. – jpierson – 2016-02-09T18:50:10.623

4

Jader Dias

Posted 2010-02-01T13:48:15.000

Reputation: 13 660

2does 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 – 2010-02-01T17:04:01.990

This is more accurate than the other answers... maybe not as useful, but more the right answer. Upvoted. – John Hunt – 2017-08-30T14:55:15.677

3

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$'

ram

Posted 2010-02-01T13:48:15.000

Reputation: 31

How is this different than already posted answers exactly? – Pimp Juice IT – 2017-10-09T16:10:03.033

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.

mpez0

Posted 2010-02-01T13:48:15.000

Reputation: 2 578

2

Why does everyone expect you to know the PID? Here's the human-friendly way:

pwdx `pgrep ###process_name###`

moodboom

Posted 2010-02-01T13:48:15.000

Reputation: 709

The question states “as I can see with ps”, so it will probably display the PID – Scz – 2017-04-09T13:24:09.467

Ah ok true. I still find this to be a quicker one liner in many of my use cases. – moodboom – 2017-04-09T15:41:07.830

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. PID is the process.

jarno

Posted 2010-02-01T13:48:15.000

Reputation: 173