Under Linux I often use /proc/<pid>/fd/[0,1,2]
to access std[in,out,err]
of any running process.
Is there a way to achieve the same result under FreeBSD and/or macOS ?
Under Linux I often use /proc/<pid>/fd/[0,1,2]
to access std[in,out,err]
of any running process.
Is there a way to achieve the same result under FreeBSD and/or macOS ?
See this StackOverflow link for a dtrace based answer to this. I've tested it on FreeBSD and it works perfectly:
capture() {
sudo dtrace -p "$1" -qn '
syscall::write*:entry
/pid == $target && arg0 == 1/ {
printf("%s", copyinstr(arg1, arg2));
}
'
}
procfs is also available in FreeBSD but from the man page it seems that fd
is not implemented. Procfs is usully only used in FreeBSD for Linux compability.
The native replacement of procfs on FreeBSD is procstat which you can use with the -f
parameter to get the file descriptor:
procstat -a -f
Another native tool to identify active files is fstat. Remember -m
for memory mapped files if needed.
fstat -m
If you like to work through the file system then we have devfs which exposes fdescfs and you can access fd in the manner you request.
ls /dev/fd
Example:
echo -n Hello > hello.txt
echo " World!" | cat hello.txt /dev/fd/0
If you want something which is portable across system to Mac as well - then you could use the tool lsof (LiSt Open Files) which available as a package.
If portability is of concern you should read Portability of file descriptor links. It covers all bases.
procstat
as a replacement for procfs
seems inaccurate to me. The initial commit for it here described it as providing "some
of the missing functionality from procfs(4) and new functionality for
monitoring and debugging specific processes." It wasn't meant to replace procfs
, which existed in FreeBSD then and still exists.
– Steve Wills
May 12 '17 at 20:24
watch /dev/pts/0
or cat /dev/pts/0 | tee -a pts0log
. Or going Python with psutil psutil.Process(pid=PID).get_open_files()
. Understand the comment from Steve why he thought calling `procstat´ a replacement was inaccurate. It is not a 1:1 replacement.
– Claus Andersen
May 24 '17 at 15:06