Since every process (except the very first one) in a Linux system has a parent, it sometimes makes things easier to understand if all processes are displayed in a tree structure. You’ll be glad to know there exists a command line utility – dubbed pstree – that displays a tree of processes.

In this tutorial, we will discuss the basics of the pstree command using some easy-to-understand examples. But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 22.04 LTS machine.

Linux pstree command

As already mentioned in the beginning, the pstree command displays a tree of processes. Following is its syntax:

pstree [options]

And here’s what the tool’s man page says about it:

       pstree shows running processes as a tree.  The tree is rooted at either

       pid  or  init  if  pid  is  omitted.   If a user name is specified, all

       process trees rooted at processes owned by that user are shown.

       pstree visually merges identical branches by  putting  them  in  square

       brackets and prefixing them with the repetition count, e.g.

           init- -getty

                |-getty

                |-getty

                `-getty

       becomes

           init---4*[getty]

       Child  threads  of a process are found under the parent process and are

       shown with the process name in curly braces, e.g.

           icecast2---13*[{icecast2}]

       If pstree is called as pstree.x11 then it will prompt the user  at  the

       end of the line to press return and will not return until that has hap?

       pened.  This is useful for when pstree is run in a xterminal.

       Certain kernel or mount parameters, such  as  the  hidepid  option  for

       procfs,  will  hide information for some processes. In these situations

       pstree will attempt to build the tree without this information, showing

       process names as question marks.

Following are some Q&A-styled examples that should give you a good idea on how the pstree command works:

Q1. How to use pstree command?

Basic usage is simple: all you have to do is to execute ‘pstree’ sans any option.

pstree

<img alt="How to use pstree command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-basic.png6319f3784995b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="526" loading="lazy" src="data:image/svg xml,” width=”500″>

So you can see this is how pstree produces process-related information in output.

Q2. How to make pstree include command line arguments in output as well?

This can be done using the -a command line option.

pstree -a

<img alt="How to make pstree include command line arguments in output as well" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-a-option.png6319f37869c55.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="513" loading="lazy" src="data:image/svg xml,” width=”500″>

So you can see the pstree command now also displays command line options for some processes.

Q3. How to force pstree to expand identical subtrees in output?

By default, the pstree command merges identical branches by putting them in square brackets and prefixing them with the repetition count. Something like this:

<img alt="How to force pstree to expand identical subtrees in output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-merge-default.png6319f378c0f8e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="41" loading="lazy" src="data:image/svg xml,” width=”166″>

However, if you want, you can force the tool to expand identical trees, something you can do using the -c command line option.

pstree -c

<img alt="Expanded subtrees" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-c-option.png6319f37916420.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="88" loading="lazy" src="data:image/svg xml,” width=”165″>

Q4. How to make pstree highlight a specific process?

In case you want the tool to highlight a specific process in output, use the -H command line option.

pstree -H [PID]

Where PID is the ID of the process you want to highlight. For example, I highlighted the firefox process on my system using the following command:

pstree -H 3124

<img alt="How to make pstree highlight a specific process" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-H-option.png6319f379693b6.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="116" loading="lazy" src="data:image/svg xml,” width=”406″>

So you can see the name ‘firefox’ is slightly highlighted compared to other names.

Q5. How make pstree show process group IDs in output?

For this, use the -g command line option.

pstree -g

<img alt="How make pstree show process group IDs in output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-g.png6319f379c1c9b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="525" loading="lazy" src="data:image/svg xml,” width=”454″>

So you can see that process group IDs are shown as decimal numbers in parentheses after each process name.

Q6. How to make pstree sort processes based on PIDs?

By default, pstree sorts processes with same ancestor by name. However, if you want, you can have pstree sort processes by PIDs as well, something which you can do using the -n command line option.

pstree -n

Note that this type of sorting is also known as numeric sort.

Q7. How to make pstree display process tree specific to a user?

If you want pstree to display all process trees rooted at processes owned by a specific user, then all you have to do is to pass the name of that user as input to the command.

For example,

pstree himanshu

So in this case, following output was produced on my system:

<img alt="How to make pstree display process tree specific to a user" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-user.png6319f37a465fc.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="550" loading="lazy" src="data:image/svg xml,” width=”500″>

Q8. How to restrict Pstree to a specific process?

If you want pstree to display only the parent and child info for a specific process, use the -s option.

pstree -s [PID]

For example, I wanted to limit the Pstree output to only the firefox process on my system, so I executed the following command:

pstree -s 3124

And here’s the output the tool displayed:

<img alt="How to restrict Pstree to a specific process" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/pstree-s-option.png6319f37abb05f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="463" loading="lazy" src="data:image/svg xml,” width=”500″>

Conclusion

Depending on the kind of work you do, the pstree command can be of great help. Here, in this tutorial, we have discussed some key command line options this tool offers. For more info, head to its man page.