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
- The proc folder is not stored on any disk. The kernel creates each file when you read it.
- Each running process has a folder in proc, named by its process ID.
- The cmdline file shows the command and arguments a process was started with.
- The status file shows the name, the state, the owner and the memory a process uses.
- The cwd, exe and fd entries show the working folder, the program file and the open files.
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
cat /proc/self/status | head -n 4The 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.
tr '\0' ' ' < /proc/4321/cmdline; echoThe 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
ls -l /proc/4321/cwd /proc/4321/exeThe cwd and exe entries are links. Listing them shows the folder the process is working in, and the program file it runs from.
sudo ls -l /proc/4321/fdThe 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
- The process has ended: its folder disappears as soon as the process ends, so read it while the process runs.
- Other users' folders are closed: reading another user's fd folder needs sudo, and some files need root.
- The values move: each read gives a fresh value, so read again before you decide anything.
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.
Show the first lines of the status file for this shell, including its name and its state.
cat /proc/self/status | head -n 4What you should see: The first lines of the status file appear, starting with the name of this shell.
Print the full command process 4321 was started with. Its arguments are shown separated by spaces.
tr '\0' ' ' < /proc/4321/cmdline; echoWhat you should see: The full command appears on one line, as the process was started.
Show the working folder and the program file of process 4321. Both are links.
ls -l /proc/4321/cwd /proc/4321/exeWhat you should see: Two links appear: one to the working folder, and one to the program file that runs.
List the files process 4321 has open. Each one appears as a numbered link.
sudo ls -l /proc/4321/fdWhat 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.