The /proc Filesystem: Your Kernel Keeps a Diary and Leaves It Open

Your kernel writes down everything: every process, every open file, how long it's been awake. It keeps it all in /proc, and it never locks the drawer. A tour of the proc filesystem, including how to rescue a file you already deleted.

Tux Harlow

4 min read

Your Linux kernel keeps a diary. Every process it's running, every file each one has open, how long it's been awake, how much memory it has left, what it thinks of your CPU. All of it is written down in real time.

And it leaves the diary open on the kitchen table, in a folder called /proc.

This is worse than the time I found my roommate's journal and it was just forty pages of him rating sandwiches. "Tuesday. Turkey club. 6 out of 10. The bread knows what it did." Except the proc filesystem is actually useful, and nobody gets hurt reading it.

Wait, they're not real files?

Nope. Nothing in /proc lives on a disk. The kernel makes up each file's contents at the moment you read it. That's why ls -l says most of them are 0 bytes, and then cat prints a whole page. It's the TARDIS of filesystems: bigger on the inside, and slightly unsettling.

The machine's vital signs

Bash
cat /proc/uptime
350211.47 1302455.10

The first number is seconds since boot (about four days here). The second is idle time added up across every CPU core. That's why it's bigger: four bored cores rack up idle seconds four times as fast. Like four teenagers on a family road trip.

Bash
cat /proc/loadavg
grep MemAvailable /proc/meminfo
grep -c ^processor /proc/cpuinfo

That's load average, the memory you can actually use, and the number of CPUs. top, free and uptime are basically just polite people reading these files out loud for you.

Every process gets a folder

Every running process has a numbered directory named after its PID. $$ is your own shell's PID, so let's snoop on ourselves:

Bash
ls /proc/$$/
cat /proc/$$/status | head

status has the name, state, owner, memory use and much more, all in plain text. Want the exact command a process was started with? It's in cmdline, with the arguments separated by NUL characters, which is why it looks like one big smushed word. Un-smush it:

Bash
tr '\0' ' ' < /proc/$$/cmdline; echo

Shortcut: /proc/self always means "whoever is reading this right now". It's a mirror. Try not to stare too long.

The party trick: un-delete a file

Here's the one that makes people at parties go quiet. Okay, Linux parties. Okay, the one Linux party.

Say a service is writing to app.log, and someone deletes it with rm. The disk space doesn't come back, because the process still has the file open, and Linux won't really delete it until the last process lets go. The file is gone from the folder, but it isn't gone.

Find it:

Bash
ls -l /proc/<PID>/fd/

Each entry is a file that process has open. The deleted one shows up as:

4 -> /var/log/app.log (deleted)

Copy it back from beyond the grave:

Bash
cp /proc/<PID>/fd/4 /var/log/app.log.rescued

That's the whole file, contents and all. You're basically a necromancer now. Put it on your CV. "Skills: Python, SQL, raising the dead (logs only)."

The same trick explains a classic mystery: df says the disk is full, but du can't find what's filling it. A process is hanging on to a big deleted file. ls -l /proc/*/fd/ 2>/dev/null | grep deleted will find the culprit.

The kernel's settings, live

/proc/sys/ holds the kernel's tunable settings as files. Reading one tells you the current value:

Bash
cat /proc/sys/net/ipv4/ip_forward
cat /proc/sys/fs/file-max

You can write to them as root and the kernel changes its behaviour instantly. Powerful. Also exactly the kind of power that gets you a very long afternoon. Changes made this way vanish at reboot. To make them stick, use sysctl and a file in /etc/sysctl.d/. sysctl net.ipv4.ip_forward reads the very same setting by its dotted name.

Rules of the diary

  • Some pages are private. /proc/<PID>/environ holds a process's environment variables (and sometimes its secrets), so only the owner and root can read it. Good.
  • Read freely, write carefully. Reading /proc is harmless. Writing to /proc/sys is surgery.
  • It's live. Read the same file twice and you may get two answers. That's not a bug. Things change. Like sandwich ratings.

Next time a tool tells you something about your system, have a peek at where it got it from. Nine times out of ten, it's been reading the kernel's diary too. The tenth time, it's making things up. That's what /proc is for.