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
2009-07-21 10:45:19
It should be noted that the `/proc` filesystem is not necessarily available in all installations of FreeBSD.
Greg Hewgill
2009-07-21 10:47:27
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
2009-07-21 15:10:40
+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
2009-07-21 15:20:49
+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
2009-11-10 18:24:51
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
2010-08-01 21:05:27