← Full reference

/proc

Virtual FS with kernel data — a process's arguments, environment and open files, memory, load

/proc isn't real files on disk but a window into the kernel. Every process appears as a directory /proc/<PID>/, and files like /proc/meminfo describe the system as a whole. The content is generated at read time, so most files have size 0, and you read them with cat, grep, tr rather than by size. This is where ps, top, free and pgrep get their data.

Syntax

cat /proc/<PID>/file
ls -l /proc/<PID>/fd
cat /proc/file

/proc/self is a link to the directory of the process reading it: ls -l /proc/self/fd shows the descriptors of ls itself. $$ in bash is the PID of the current shell.

Process directory /proc/<PID>/

FileWhat's inside
cmdlinecommand line; arguments are separated by a NUL byte, not a space
commprocess name (up to 15 characters) — what pgrep without -f searches
environenvironment variables at startup, separated by \0; only the owner or root can read it
statushuman-readable summary: Name, State, PPid, Uid, Gid, VmRSS, Threads, SigIgn…
fd/open descriptors: symlinks 0, 1, 2, 3… to files, sockets (socket:[inode]), pipes (pipe:[…])
fdinfo/Nposition and flags of open descriptor N
cwdsymlink to the process's current directory
exesymlink to the executable (even a deleted one — marked (deleted))
rootsymlink to the process's root (differs under chroot)
mapsmemory map: which libraries and files are mapped
limitslimits: open files, processes, core size, etc.
iohow many bytes the process has read and written
statsame as status, on one line for programs (fields by position)
task/the process's threads — one directory per thread
oom_scorehow "attractive" the process is to the OOM killer (higher — killed sooner)
cgroupwhich cgroup the process is in

/proc/<PID>/status fields

FieldMeaning
Nameprocess name
StateR, S, D, Z, T — as in ps
PPidparent PID
Uid / Gidreal, effective, saved, filesystem ID
VmRSSphysical memory in use; VmSize — virtual; VmPeak — peak
Threadsnumber of threads
SigIgn / SigCgtbitmasks of ignored / caught signals (bit N−1 — signal N)
voluntary_ctxt_switcheshow many times the process gave up the CPU on its own (waiting for I/O)

System files

FileWhat's inside
/proc/loadavgload average over 1, 5, 15 minutes; running/total threads; last PID
/proc/meminfomemory: MemTotal, MemAvailable (what is really available), Cached, SwapFree
/proc/cpuinfoprocessors; number of cores — grep -c ^processor or nproc
/proc/uptimeseconds since boot and total idle time of all CPUs
/proc/mountsmounted file systems (like mount with no arguments)
/proc/swapsactive swap
/proc/versionkernel version
/proc/net/tcp, /proc/net/udpsockets in hexadecimal form (ss is more convenient)
/proc/sys/…kernel parameters; same as sysctl (/proc/sys/fs/file-max = fs.file-max)

Examples

# a process's command line with spaces instead of \0
tr '\0' ' ' < /proc/1234/cmdline; echo

# each argument on its own line
xargs -0 -n1 < /proc/1234/cmdline

# a process's environment, one variable per line
tr '\0' '\n' < /proc/1234/environ

# parent, state and memory
grep -E '^(Name|State|PPid|VmRSS)' /proc/1234/status

# which files a process has open and how many
ls -l /proc/1234/fd
ls /proc/1234/fd | wc -l

# where the process "stands" and which binary it is
readlink /proc/1234/cwd /proc/1234/exe

# free memory and load
grep MemAvailable /proc/meminfo
cat /proc/loadavg

A deleted but still open file

If you delete a file that a process holds open, the name disappears, but the data stays on disk until the process closes the descriptor. The classic symptom: df says the disk is full, but du can't find that many files. There is no lsof in the sandbox, but /proc gives you the same:

# all descriptors pointing to deleted files — the PID is visible in the path
ls -l /proc/*/fd/* 2>/dev/null | grep '(deleted)'
# l-wx------ 1 root root 64 … /proc/7/fd/3 -> /tmp/data.txt (deleted)

# the same via find, more compact
find /proc/[0-9]*/fd -lname '*(deleted)' -printf '%p -> %l\n' 2>/dev/null

# get the contents of the deleted file while the process is alive
cp /proc/7/fd/3 /tmp/recovered

# free the space without stopping the process — truncate the file via the descriptor
: > /proc/7/fd/3

The space is finally freed when the process closes the file — after a restart, or on a signal if the program can reopen its logs.

Pitfalls

  • cat /proc/PID/cmdline glues the arguments together: the separators are \0 and the terminal doesn't show them. Use tr '\0' ' ' or xargs -0.
  • environ is the environment at startup. Variables the process changed for itself later are not reflected there.
  • Write ls -l /proc/*/fd/*, not /proc/*/fd. In the second case the PID ends up in separate directory headers, and grep cuts it off.
  • A process can disappear mid-read. Errors like No such file or directory while walking /proc/* are normal; silence them with 2>/dev/null.
  • Other users' fd, environ, cwd are visible only to the owner or root.
  • /proc/sys in a container is usually read-only: echo 1 > /proc/sys/… returns Read-only file system. Kernel parameters for a container are set from outside.
  • In a container /proc/meminfo and /proc/cpuinfo often show the whole host's resources, not the container's limits.

See also

ps · pgrep · kill · df-du · ss