pstree Command in Linux [with Examples]

Written by: Bobbin Zachariah   |   Last updated: November 11, 2022

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]

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 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.

OptionsDescription
defaultDisplay processes in tree-like structure
-aDisable compaction only for processes but not threads
-ADisplay the tree using ASCII characters
-cDisplay identical subtrees separately
-gDisplay PGIDs (process group IDs) in decimal form after every process names 
-GUse VT100 video terminal characters
-hHighlight the parents and sub-branches of a process
-lWrap the output for a good view
-nSort the process using their PID’s
-pDisplay PID’s in parentheses after each process
-sDisplay parent processes of specified process
-SDisplay namespaces transitions
-tDisplay full names of process’s threads
-TDisplay only processes hiding the threads
-uDisplay uid if it differs from its parent in parentheses
-UDisplay output in UTF-8 (Unicode) line-drawing characters
-VDisplay Package’s Version Information
-ZDisplay security attributes of the current process

Install pstree

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 psmisc
Fedora / Red Hat / CentOS Steam
yum install psmisc
macOS
brew install pstree

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
output of 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
pstree show arguments

Identical subtrees

If you want you can output identical subtrees separately, using:

pstree -c
pstree identical subtrees

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
show process group id

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
pstree showing the current process

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
Display long lines

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
Sort processes with the same parent by PID instead of by name.

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
pstree show pid

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
Show full names for threads

Hide the threads

If only interested in process names while hiding the threads.

pstree -T
hide threads and only show processes

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 uid transitions

Show the current security attributes of the process

To view the security context of each process owned.

pstree -Z
Display the current security attributes of the process

For example, SELinux would be security context.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment