views:

196

answers:

4

I have the pid of a running process in freebsd. How do I obtain its current directory?

A: 

/proc/$PID/cwd contains a symlink to the programmes current working directory.

ott
It should be noted that the `/proc` filesystem is not necessarily available in all installations of FreeBSD.
Greg Hewgill
This works for Linux, but doesn't seem to be part of /proc in FreeBSD. See the procfs man page at: http://www.freebsd.org/cgi/man.cgi?query=procfs
Keith Smith
+1  A: 

You can get the inode number of the current working directory with

fstat -p $PID

It looks like lsof will give you a human readable form of the current working directory, but we don't have that installed on any of the local FreeBSD machines, so I can't verify that.

Keith Smith
+1  A: 

fstat can find the inode number and filesystem, and find can find the correct directory.

Try this:

fstat -p $$|perl -ane '$F[3] eq "wd" && system("find",$F[4],"-xdev","-inum",$F[5],"-print");'

When run as non-root, find will probably output quite a few "Permission denied" messages which can be avoided by inserting close(STDERR); in front of the $F[3] (after the first single quote).

Sec
A: 

In more recent versions of FreeBSD, you can use procstat -f $PID, which shows the pathname if it is still in the kernel name cache.

jilles