All articles

Inspect a Linux Process Through /proc/PID/fd

7 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Inspect a Linux Process Through /proc/PID/fd

A process keeps small integer descriptors that refer to kernel open-file descriptions, pipes, sockets, devices, and other objects. Linux exposes this mapping under /proc/PID/fd and more detail under /proc/PID/fdinfo.

The lab below was tested on Ubuntu 24.04.4 with Linux 6.8. It writes one file in a fresh temporary directory and closes the descriptor before cleanup.

Inventory before reading anything

pid=12345
ls -l -- "/proc/$pid/fd"

Replace 12345 with a current, confirmed PID. Entries 0, 1, and 2 are normally standard input, output, and error. Other numbers are application resources. Each entry is a symbolic link; pipes and sockets use forms such as pipe:[inode] and socket:[inode] 1.

Use readlink for a machine-focused view:

for fd_path in "/proc/$pid"/fd/*; do
  printf '%s -> %s\n' "${fd_path##*/}" "$(readlink "$fd_path")"
done

The directory can change while you iterate because another thread may open or close a descriptor. Treat the result as a snapshot, not a transaction.

Read fdinfo, not the stream

sed -n '1,8p' "/proc/$pid/fdinfo/3"

For a regular file, fdinfo includes the current offset, octal status flags, mount ID, and inode. Other descriptor types add fields: eventfd counters, epoll targets, signal masks, or inotify watches 2.

Caution: Do not cat /proc/PID/fd/N merely to inspect it. Opening that path can read from a pipe, terminal, device, or socket, consume data, block, trigger side effects, or expose secrets. Start with readlink and fdinfo.

Access is permission-controlled. Being able to list a process does not guarantee you may dereference every descriptor; /proc mount options and ptrace checks also matter 1.

The verified descriptor lab

lab=$(mktemp -d)
sh -c '
  exec 3>"$1/fd.log"
  printf "fd 3 target: "
  readlink "/proc/$$/fd/3"
  sed -n "1,5p" "/proc/$$/fdinfo/3"
  exec 3>&-
' sh "$lab"

Observed output:

fd 3 target: /tmp/tmp.example/fd.log
pos:    0
flags:  0100001
mnt_id: 29
ino:    71669

Inode and mount IDs will differ. The exec 3> redirection created a write-only descriptor, and exec 3>&- closed it. A successful open() returns a nonnegative descriptor that indexes the process’s descriptor table; the underlying open-file description stores the offset and status flags 3.

Diagnose common patterns

A deleted file still using space

ls -l "/proc/$pid/fd" | grep ' (deleted)$'

An unlinked regular file continues to use space while a process still references it. Confirm the process and service before restarting it; do not truncate /proc/PID/fd/N as a generic fix.

A large descriptor count

find "/proc/$pid/fd" -mindepth 1 -maxdepth 1 -printf '.' | wc -c
sed -n '/Max open files/p' "/proc/$pid/limits"

A high count is not automatically a leak. Group targets by regular file, pipe, socket, device, and anonymous inode, then compare snapshots over time and with the workload.

A socket descriptor

readlink "/proc/$pid/fd/7"
ss -anp

The socket inode can help correlate the descriptor with socket tables, while ss -p usually provides the easier ownership view when permitted.

Descriptor is not pathname

A pathname can be renamed or unlinked after open(), while the descriptor continues to refer to the same open-file description 3. Conversely, the same pathname can be opened several times with independent offsets.

Use /proc to inspect the descriptor number, target type, offset, flags, and lifetime before blaming a pathname.

Sources and further reading
  1. Linux man-pages — proc_pid_fd(5)
  2. Linux man-pages — proc_pid_fdinfo(5)
  3. Linux man-pages — open(2)