Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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).

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top