pstree command in Linux is used to display running processes owned by root or specific users in a tree-like structure. A tree is a hierarchical data structure consisting of n0des (processes) and a list of references to other nodes (sub-processes). This type of structure gives the user a better understanding of running processes in the Linux system.
Unlike ps and top command, pstree gives a better understanding/visualization of processes. A good alternative for pstree is ps auxf.
The general syntax for using the pstree command:
pstree [OPTIONS] [PID or USERNAME]
PID or USERNAME: You can provide either a process ID (PID) or a username as an argument to display the process tree for a specific process or user.
pstree command has a variety of options you can pass with the command to achieve various outputs. You can refer to the following table.
|Display processes in tree-like structure
|Disable compaction only for processes but not threads
|Display the tree using ASCII characters
|Display identical subtrees separately
|Display PGIDs (process group IDs) in decimal form after every process names
|Use VT100 video terminal characters
|Highlight the parents and sub-branches of a process
|Wrap the output for a good view
|Sort the process using their PID’s
|Display PID’s in parentheses after each process
|Display parent processes of specified process
|Display namespaces transitions
|Display full names of process’s threads
|Display only processes hiding the threads
|Display uid if it differs from its parent in parentheses
|Display output in UTF-8 (Unicode) line-drawing characters
|Display Package’s Version Information
|Display security attributes of the current process
pstree is installed by default in a Linux-based operating system, therefore is no need to install it manually. However, if it is not present in your system you can easily install it with the following command depending on which OS you have.
Ubuntu / Debian
sudo apt install psmiscFedora / Red Hat / CentOS Steam
yum install psmiscmacOS
brew install pstree
Let's have a look at different examples using the available pstree options.
Display process in a tree structure
To display a tree of all the processes hierarchy simply use the following command.
To read the output of pstree, in the example systemd is the root node here and others like Network manager, bluetoothd, etc are child and further and these child nodes act as parent nodes for remaining sub-processes and so on. This gives us the overall structure of terminal nodes that really belong to which process groups.
Show command line arguments
To display the command line arguments for the processes in the terminal.
If you want you can output identical subtrees separately, using:
Show the process group IDs (PGIDs)
Process group ids are assigned automatically in Linux for every process. Each id is unique. You can view all the process group ids using:
PGIDs are displayed in parentheses after each process. Once you get the process id you can further investigate more on the running program.
Highlights the current process
As there are many processes running in a system you should highlight the current processes (parent and its branches) using -
In the above image "systemd" is in bold which makes it easy to understand the parent process.
The highlighted processes show the child processes after the parent processes.
A better view of pstree
Sometimes a process has too many branches and children which gets cut to the edge of the terminal and is not properly visible. To avoid these you can wrap the whole content just like you wrap text in a text editor using
Sort the processes by numeric sort(using PID)
When you run the pstree command in Linux, by default it gives you the output in sorted order of the process name. You can sort processes by their PIDs using -
You can verify that by passing -g argument with the -n argument to also view the PGIDs while sorting them at the same time.
Displays PIDs for each process name
PIDs for each and every process can be displayed using:
Process ID are present in parentheses after each process name. This is a good screen to look for running parent processes with their child processes along with its PIDs.
Show full names for threads
A thread is the smallest process and can do one or more units of work. If you are curious to know all the processes and the threads you can display the full names of the threads using:
Hide the threads
If only interested in process names while hiding the threads.
Show uid transitions
Display the uid (user identifier) by passing -u. When a process's uid differs from that of its parent, the new uid is displayed in parenthesis following the process name.
Show the current security attributes of the process
To view the security context of each process owned.
For example, SELinux would be security context.