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

262,174

Solution 1

% 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

Solution 2

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

Solution 3

One way is ps -ef

Solution 4

ps auxwwwe

Source:

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

Solution 5

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$'
Share:
262,174
Jader Dias
Author by

Jader Dias

Perl, Javascript, C#, Go, Matlab and Python Developer

Updated on September 17, 2022

Comments

  • Jader Dias
    Jader Dias over 1 year

    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
    Jader Dias over 14 years
    didn't work for a specific service, it just provide the relative path
  • akira
    akira over 14 years
    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
  • jpierson
    jpierson over 8 years
    Helped me identify a process via the command it was started with.
  • noamik
    noamik over 8 years
    @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.
  • jarno
    jarno about 8 years
    In my system (ubuntu 14.04) you do not have to be superuser to run the ls command.
  • Irfy
    Irfy about 8 years
    @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.
  • Daniel Da Cunha
    Daniel Da Cunha over 7 years
    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.
  • Scz
    Scz about 7 years
    The question states “as I can see with ps”, so it will probably display the PID
  • moodboom
    moodboom about 7 years
    Ah ok true. I still find this to be a quicker one liner in many of my use cases.
  • John Hunt
    John Hunt almost 7 years
    This is more accurate than the other answers... maybe not as useful, but more the right answer. Upvoted.
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style over 6 years
    How is this different than already posted answers exactly?
  • John Strood
    John Strood almost 6 years
    @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?"
  • Nick Dong
    Nick Dong over 5 years
    pwdx return me the absolute path of the exectuable program of the process depending on pid on redhat x64 6.3.
  • electrovir
    electrovir about 4 years
    AFAICT this isn't using the PID at all and just errors out
  • jarno
    jarno about 4 years
    @electrovir fixed the description.
  • Peter Mortensen
    Peter Mortensen over 3 years
    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?
  • Blazej SLEBODA
    Blazej SLEBODA over 3 years
    this one can be executed on iOS
  • Peter Mortensen
    Peter Mortensen over 3 years
    This does not answer the question at all.
  • David Moles
    David Moles over 3 years
    This tells you the current working directory of the process, not the path to its executable.
  • moodboom
    moodboom over 3 years
    Good point, they may be different.