Pstree Command in Linux Terminal with Examples

Pstree Command in Linux Terminal with Examples

In this article we are going to shows how to use pstree command in Linux. If you want to learn about pstree command in Linux with Examples then this post is ideal for you.

Linux is a powerful multi-tasking and multi-user operating system. It allows multiple processes to operate simultaneously without any interfering with each other.

A process is an running instance of a program and it is a most important fundamental concept of the Linux operating system.

In the Linux, pstree is a command line utility that is used to show information about running processes as a tree. The pstree is similar to ps command, but instead of listing the information about running processes, it shows them in a tree format. It is a more convenient way to show the processes hierarchy.


How to Use pstree command in Linux

In the Linux, the basic syntax of the pstree command is show as below:

pstree [OPTIONS] [USER or PID]

When you run the pstree command without any options then it will display the hierarchical tree structure of all running processes on your system.

pstree
Output

systemd─┬─accounts-daemon───2*[{accounts-daemon}]
        ├─2*[agetty]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─do-agent───5*[{do-agent}]
        ├─fail2ban-server───3*[{fail2ban-server}]
        ├─lvmetad
        ├─lxcfs───5*[{lxcfs}]
        ├─master─┬─pickup
        │        └─qmgr
        ├─nginx───3*[nginx]
...

pstree command merge identical tree branches by putting them between square brackets and set prefixing them with an integer that shows the number of branches. For an example:

├─2*[agetty]

The above output is similar to the below:

├─agetty
├─agetty

If you want to disable the merging the branches, use the -c option with pstree command as show below:

pstree -c

Basically when you run pstree command, it will display the output in multiple lines that does not fit on your system screen

Generally, the output of the pstree is in multiple lines so output doesn’t fit on the screen. To display the output one page at a time, pipe  less command as following:

To view the output of pstree one page at a time, pipe pstree command to the less command:

pstree | less

View the processes of a particular user

If you want to check the running processes of a particular user, use the username as an argument with the pstree command as show below:

pstree studiesonline

The above command will display only the processes owned by the given user “studiesonline“.

If you use PID as an argument, pstree command prints a tree with the given process as the root of the tree.

pstree 1892
Output
sshd───bash───pstree

To show the parent processes of the given process : Use -s option with the process PID to show the parent processes of the given process as show below:

pstree -s 1892
Output
systemd───sshd───sshd───bash───pstree

Show PIDs and PGIDs 

Use -p option to show running process with PIDs by running below command:

pstree -p

The above command will display the hierarchical tree structure of all running processes with their process IDs.

By default, the pstree command sorts the all running processes with the same parent by name, but if you want to sort them by PIDs (numeric sort) then use -n option with pstree command as show below:

pstree -pn

The PGIDs (process group ID) is the process ID of the first member of the process group. Use the -g option to display PGIDs:

pstree -g

Highlighting Processes

To highlight the current process and all its ancestors, use the -h option with pstree command:

pstree -h

Use -H option with the process ID to highlight a specific process as show below:

pstree -H PID_NUMBER

That’s all.

If you face any error and issue in above steps , please use comment box below to report.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top