/proc Talk

The Linux /proc virtual filesystem offers a window into a running system – look inside for information on processes and kernel activity.

The proc filesystem [1] (procfs for short), is a Linux pseudo-filesystem that provides an interface to the operating system's kernel data structures. Procfs leverages the well-known concept of "in Unix, everything is a file" [2] to provide the same uniform interface of Unix file I/O (e.g., open, read, write, close, etc.) for getting kernel- and OS-related information. This uniformity makes it easier for the Linux programmer or system administrator to learn about the kernel, with fewer interfaces to learn.

Procfs is usually mounted at /proc. Many kinds of information about the operating system (OS) and processes running in it are exposed via pseudo-files in procfs. By reading data from /proc files, you can learn a lot of useful information about the system.

This article shows some ways of getting information from procfs using custom Python programs and Linux commands.

For comprehensive details about the information each pseudo-file in procfs provides, please refer to the man page for the /proc filesystem [3]. I will focus on showing practical examples, along with a few fun shell features and tricks.

More on procfs

Procfs is automatically mounted by the kernel at boot time under the /proc mountpoint.

The fragment (since Linux 3.3)

hidepid=n

specifies a mount option. The values for n can be , 1, or 2, with being the most lenient, and 1 and 2 being progressively stricter modes of access with respect to security and privacy of information. Mode is the default.

Many categories of files and directories reside under the /proc hierarchy. Each category serves a different purpose.

Table 1 shows the categories of proc files and what the kind of information each type provides. (In Table 1 and in the examples below, [pid] is a placeholder for the process ID of the process for which you want information.)

Table 1

Some procfs File Categories

/proc/[pid]/cmdline

Get the command line of a process

/proc/[pid]/environ

Get the environment of a process

/proc/[pid]/status

Get the status of a process

/proc/meminfo

Get the memory information of a computer

Procfs provides two kinds of OS information: process-specific and non-process-specific, or general. The first three items in Table 1 are process-specific information, and the last item is an example of general OS information.

In the rest of this article, I reveal the details of the kind of information each of the categories in Table 1 contains, as well as some examples of what you can do with this information.

Get the Number of Numerically Named Directories

Procfs contains a numerically named subdirectory for each running process; the subdirectory is named by the process ID (PID):

/proc/[pid]

The number of numerically named directories (each representing a running process named by the process's PID) is an indication of the number of running processes:

$ ls -ld /proc/[0-9]* | wc -l
84

The command means: list (ls) the files matching the given file name pattern, and count the number of lines in the output (wc -l). Only the directory names (the d in -ld) are listed in long format (the l in -ld). Only files with names made up of just one or more digits ([0-9]*) are counted.

Get the Command Line of a Process

Files of the form

/proc/[pid]/cmdline

hold the complete command line for the process. The components of the command line appear in this file as a sequence of strings, each terminated by a null byte (ASCII code 0, which is character \0).

Figure 1 shows how to get the command line of a process with a Python script. The get_proc_cmdline.py script is shown in Listing 1. The script takes a PID as input and outputs the command line for the process, as referenced in the /proc/[pid]/cmdline file. As you can see in Listing 1, get_proc_cmdline.py refers to proc_info.py (Listing 2) and error_exit.py (Listing 3).

Figure 1: Getting the command line of a process using a Python script.

The next example uses echo_args.py, a Python program that echoes its arguments to the standard output

To see get_proc_cmdline.py at work, consider the example script in Listing 4, echo_args.py, which is a test script that echoes the command-line arguments used to call the script to standard output, then sleeps for a while, giving the user some time to run another program that reads the command line of the process running echo_args.py. Run echo_args.py with arguments, using some combinations of backslashes and quoting to show how they are interpreted by the shell before the Python program receives them. A trailing ampersand & after the command starts a new background process and outputs the process ID. For example,

Tip: The value of the $! built-in shell variable is the PID of the last background process started, so if you are sure that the command python echo_args.py is the last background process run on this terminal, you can use $! instead of the literal value 2943 for the PID.

Klaus Knopper is the creator of Knoppix and co-founder of LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com