Linux pstree Command Tutorial for Beginners (8 Examples)

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

How to use pstree command

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

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

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:

How to force pstree to expand identical subtrees in output

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

Expanded subtrees

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

How to make pstree highlight a specific process

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

How make pstree show process group IDs in output

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:

How to make pstree display process tree specific to a user

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:

How to restrict Pstree to a specific process

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.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment