/proc/self: Explanation & Insights

Information about the current process

In a Unix-like operating system, /proc is a virtual filesystem that provides a way to access information about processes and the system itself. The /proc/self directory is a symbolic link that refers to the process directory of the current process. Here are some examples of how /proc/self can be used:

Get the process ID (PID) of the current process:

cat /proc/self/`pidof $$`/status | grep PID
PID:    1234

In this example, the pidof $$ command gets the PID of the current process, and the cat command reads the status file in the process directory to get information about the process, including the PID.

Read the command line arguments of the current process:

cat /proc/self/cmdline
/usr/bin/python3 script.py arg1 arg2

The cmdline file contains the command line arguments passed to the process, separated by null characters.

Get information about the memory usage of the current process:

cat /proc/self/status | grep VmPeak
VmPeak:   123456 kB

The status file contains information about the memory usage of the process, including the peak virtual memory usage (VmPeak).

Access file descriptors of the current process:

ls -l /proc/self/fd
lr-x------ 1 user user 64 Apr 16 10:00 0 -> /dev/pts/0
lrwx------ 1 user user 64 Apr 16 10:00 1 -> /dev/pts/0
lrwx------ 1 user user 64 Apr 16 10:00 2 -> /dev/pts/0

The fd directory contains file descriptors for the current process. In this example, file descriptors 0, 1, and 2 are all linked to the same terminal device /dev/pts/0.

Overall, /proc/self provides a way for a process to access information about itself and its environment, which can be useful for debugging, performance monitoring, and other system-level tasks.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA