In Linux, there are lots of commands available for process management. In the tutorial, we learn about pstree command which will help you better understand the system processes hierarchy.
pstree command
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.
Syntax
The general syntax for using the pstree command:
pstree [options] [pid or username]
Install pstree
pstree is installed by default in a Linux-based operating system, therefore is no need to install it manually.
Although, 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 psmisc
Fedora / Red Hat / CentOS Steam
yum install psmisc
macOS
brew install pstree
pstree options
pstree command has a variety of options you can pass with the command to achieve various outputs. You can refer to the following table.
Options | Description |
---|---|
default | Display processes in tree-like structure |
-a | Disable compaction only for processes but not threads |
-A | Display the tree using ASCII characters |
-c | Display identical subtrees separately |
-g | Display PGIDs (process group IDs) in decimal form after every process names |
-G | Use VT100 video terminal characters |
-h | Highlight the parents and sub-branches of a process |
-l | Wrap the output for a good view |
-n | Sort the process using their PID’s |
-p | Display PID’s in parentheses after each process |
-s | Display parent processes of specified process |
-S | Display namespaces transitions |
-t | Display full names of process’s threads |
-T | Display only processes hiding the threads |
-u | Display uid if it differs from its parent in parentheses |
-U | Display output in UTF-8 (Unicode) line-drawing characters |
-V | Display Package’s Version Information |
-Z | Display security attributes of the current process |
pstree Examples
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.
pstree
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.
pstree -a
Identical subtrees
If you want you can output identical subtrees separately, using:
pstree -c
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:
pstree -g
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 -
pstree -h
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
pstree -l
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 -
pstree -n
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:
pstree -p
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:
pstree -t
Hide the threads
If only interested in process names while hiding the threads.
pstree -T
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.
pstree -u
Show the current security attributes of the process
To view the security context of each process owned.
pstree -Z
For example, SELinux would be security context.
Conclusion
pstree command is very helpful for dealing with different processes. The visual representations just make it one of the best Linux commands.
To know more about pstree run the man pstree command in your terminal.
Navigate all-in-one place of Linux Commands for more learning.
Comments