How to find out from which folder a process is running?

15

7

In *nix you can use ps to see which process are running. But if an executable has multiple homonimous files in a device, we can't figure where it was invoked from.

This is slightly different from my last question on this subject. How can I know the absolute path of a running process?

Jader Dias

Posted 2011-06-20T16:37:53.467

Reputation: 13 660

Answers

21

Try this:

ls -l /proc/<PID>/cwd

HUB

Posted 2011-06-20T16:37:53.467

Reputation: 571

7

Derived rom HUB's answer:

readlink /proc/<PID>/cwd

or even

readlink /proc/$(pgrep <program_name>)/cwd

jpaugh

Posted 2011-06-20T16:37:53.467

Reputation: 1 212

+1 The output of readlink can be parsed better in a script. – Marco Leogrande – 2012-11-21T06:34:28.217

2

Duplicate of https://unix.stackexchange.com/questions/94357/find-out-current-working-directory-of-a-running-process ?

There are 3 methods that I'm aware of:

pwdx

$ pwdx PID

lsof

$ lsof -p PID | grep cwd

/proc

$ readlink -e /proc/PID/cwd

Florian

Posted 2011-06-20T16:37:53.467

Reputation: 121

2

You can't tell where a process was invoked from, only where it currently is. Look at the cwd ("current working directory") link instead of exe.

Ignacio Vazquez-Abrams

Posted 2011-06-20T16:37:53.467

Reputation: 100 516

1

I guess this command should work. It is a little workaround but it works at least on my machine.

for strlist in $(ps e PID);do if [ ${strlist:0:4} = "PWD=" ]; then echo ${strlist:4};fi;done

Enrico

Posted 2011-06-20T16:37:53.467

Reputation: 11

0

When I ran

ps auxwwwe | grep executableName > dump
vim dump

I was able to look for the part of the path I knew, and then I found out from which subdirectory the command was invoked from

Jader Dias

Posted 2011-06-20T16:37:53.467

Reputation: 13 660