/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>/
| File | What's inside |
|---|---|
cmdline | command line; arguments are separated by a NUL byte, not a space |
comm | process name (up to 15 characters) — what pgrep without -f searches |
environ | environment variables at startup, separated by \0; only the owner or root can read it |
status | human-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/N | position and flags of open descriptor N |
cwd | symlink to the process's current directory |
exe | symlink to the executable (even a deleted one — marked (deleted)) |
root | symlink to the process's root (differs under chroot) |
maps | memory map: which libraries and files are mapped |
limits | limits: open files, processes, core size, etc. |
io | how many bytes the process has read and written |
stat | same as status, on one line for programs (fields by position) |
task/ | the process's threads — one directory per thread |
oom_score | how "attractive" the process is to the OOM killer (higher — killed sooner) |
cgroup | which cgroup the process is in |
/proc/<PID>/status fields
| Field | Meaning |
|---|---|
Name | process name |
State | R, S, D, Z, T — as in ps |
PPid | parent PID |
Uid / Gid | real, effective, saved, filesystem ID |
VmRSS | physical memory in use; VmSize — virtual; VmPeak — peak |
Threads | number of threads |
SigIgn / SigCgt | bitmasks of ignored / caught signals (bit N−1 — signal N) |
voluntary_ctxt_switches | how many times the process gave up the CPU on its own (waiting for I/O) |
System files
| File | What's inside |
|---|---|
/proc/loadavg | load average over 1, 5, 15 minutes; running/total threads; last PID |
/proc/meminfo | memory: MemTotal, MemAvailable (what is really available), Cached, SwapFree |
/proc/cpuinfo | processors; number of cores — grep -c ^processor or nproc |
/proc/uptime | seconds since boot and total idle time of all CPUs |
/proc/mounts | mounted file systems (like mount with no arguments) |
/proc/swaps | active swap |
/proc/version | kernel version |
/proc/net/tcp, /proc/net/udp | sockets 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/cmdlineglues the arguments together: the separators are\0and the terminal doesn't show them. Usetr '\0' ' 'orxargs -0.environis 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, andgrepcuts it off. - A process can disappear mid-read. Errors like
No such file or directorywhile walking/proc/*are normal; silence them with2>/dev/null. - Other users'
fd,environ,cwdare visible only to the owner or root. /proc/sysin a container is usually read-only:echo 1 > /proc/sys/…returnsRead-only file system. Kernel parameters for a container are set from outside.- In a container
/proc/meminfoand/proc/cpuinfooften show the whole host's resources, not the container's limits.