Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a daemon process and I want to know what files it has open (and ideally what its CWD is). Is there any shell command that can tell me that?

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

I do love lsof, but I think it's overkill for a simple question like this. The /proc filesystem contains everything you want to know. Perhaps an example would be best:

# ps ax|grep tail
 7196 pts/4    S+     0:00 tail -f /var/log/messages
 8773 pts/0    R+     0:00 grep tail
# ls -l /proc/7196/cwd
lrwxrwxrwx 1 insyte insyte 0 2009-07-29 19:05 /proc/7196/cwd -> /home/insyte
# ls -l /proc/7196/fd
total 0
lrwx------ 1 insyte insyte 64 2009-07-29 19:05 0 -> /dev/pts/4
lrwx------ 1 insyte insyte 64 2009-07-29 19:05 1 -> /dev/pts/4
lrwx------ 1 insyte insyte 64 2009-07-29 19:02 2 -> /dev/pts/4
lr-x------ 1 insyte insyte 64 2009-07-29 19:05 3 -> /var/log/messages

So as you can see, the /proc/$PID directory contains a symlink called "cwd" that links the the CWD of the process. The same is true for the open filedescriptors listed in /proc/$PID/fd.

The /proc/$PID hierarchy contains a wealth of information about all running processes. Worth poking around in!

share|improve this answer
    
Great info. Thanks! –  Denis Hennessy Jul 30 '09 at 11:08
    
lsof is easier. –  Matt Mar 22 '12 at 2:17
add comment

If you have the command lsof available [whcih most *nix flavors do] you would use:

lsof -p NNN

to list files open by process NNN. I haven't used BSD in a while but from memory fuser is a close parallel to lsof.

I'm not sure of a command to find the cwd of a process but on Linux cwd is symlinked into the /proc directory of the process ie. /proc/NNN/cwd.

share|improve this answer
1  
On Ubuntu, lsof comes with the lsof package. I don't think that it's installed by default. –  innaM Jul 30 '09 at 7:55
add comment

if you know the processes PID, you can just issue an

lsof | grep YOURPID

Quick and easy to remember.

or

lsof -c yourprogramexecutable
share|improve this answer
2  
What's wrong with lsof -p PID? –  innaM Jul 30 '09 at 7:56
    
Nothing. It's only that the grep pipe is burned into my brain for at least 15 years now :) –  SvW Jul 30 '09 at 9:29
add comment

Try lsof if it's installed on your system

D

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.