What is inside the proc folder for a process

Video 127 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:26, with English and Hindi audio.

Linux shows each running process as a folder of plain files, inside a special folder called proc. When a process is busy and nobody remembers how it was started, its proc folder holds the answers. This article explains what the proc folder is, how to read the status and cmdline files, and what the cwd, exe and fd entries tell you. It also covers the limits: the folder disappears when the process ends, and other users' folders need sudo.

What you will learn

A process is a folder of plain files

A process is busy on a warehouse server, and nobody remembers how it was started. The short name in the process list is not enough. The kernel knows much more. For every process, it keeps a folder of details, and the folder is named by the process ID.

The proc folder is special. Nothing is stored on a disk there. The kernel creates each file the moment you read it. A folder named self points to whichever process is reading it, so self is always the process that runs your command.

Reading the status and the start command

Show the first lines of the status file for this shell
cat /proc/self/status | head -n 4

The status file answers what the process is doing now. It gives a name line and a state line, plus the owner and the memory the process uses. The state letters match the ones in the process list.

Print the full command process 4321 was started with
tr '\0' ' ' < /proc/4321/cmdline; echo

The cmdline file keeps the arguments apart with zero bytes, which cannot be read as text. The tr command swaps those zero bytes for spaces, so the full command can be read. Often this is the quickest way to know what a mystery job really is.

The working folder, program file and open files

Show the working folder and the program file of process 4321
ls -l /proc/4321/cwd /proc/4321/exe

The cwd and exe entries are links. Listing them shows the folder the process is working in, and the program file it runs from.

List the files process 4321 has open
sudo ls -l /proc/4321/fd

The fd folder lists everything the process has open. A file, a network connection or a pipe each appear as a numbered link. Only the owner, or root, can read another user's fd folder, so sudo is needed for a process that belongs to someone else.

What goes wrong when reading proc

Try it yourself

You need a Linux terminal, and sudo rights for the last step. Use a process ID of your own in place of 4321, and read the folder while that process is running.

  1. Show the first lines of the status file for this shell, including its name and its state.

    cat /proc/self/status | head -n 4

    What you should see: The first lines of the status file appear, starting with the name of this shell.

  2. Print the full command process 4321 was started with. Its arguments are shown separated by spaces.

    tr '\0' ' ' < /proc/4321/cmdline; echo

    What you should see: The full command appears on one line, as the process was started.

  3. Show the working folder and the program file of process 4321. Both are links.

    ls -l /proc/4321/cwd /proc/4321/exe

    What you should see: Two links appear: one to the working folder, and one to the program file that runs.

  4. List the files process 4321 has open. Each one appears as a numbered link.

    sudo ls -l /proc/4321/fd

    What you should see: The open files appear as numbered links.

Terms

proc folder
A folder that the kernel fills in as you read it, with one folder for each running process, named by its process ID.
cmdline
The file in a process folder that holds the command and arguments the process was started with.

Check yourself

Which file in a process folder shows how the process was started?

cmdline. It holds the command and the arguments, and tr makes them readable.

Why does the proc folder not use any disk space?

The kernel creates each file the moment it is read, so nothing is stored on a disk there.

Why might you need sudo to read a process folder?

Only the owner, or root, can read another user's fd folder, so a process that belongs to someone else needs sudo.

Previous: Zombie and orphan processes. Next: The sys folder: the kernel's view of devices.