pstree command in Linux

pstree command is a Linux command which displays the running processes as a tree. It is a visual alternative to another similar command which is called the ps command in Linux. The root of the visual tree output from this command is either the init or the process with the given pid.

Difference between ps and pstree

Both ps and pstree commands display the running processes which are currently active in the logged-in system. But the difference between ps and pstree is that while using pstree, the processes are organized in a tree-shaped manner rather than in the form of a list.  This tree displays the active processes in a parent-child relationship.

This tree structure makes it easy to navigate through the processes and understand the path of each process along with the parent process for every child process. This structure in pstree is similar to the hierarchical directories in the Unix systems.

Syntax

Let us have a look at the syntax of the pstree command below:

pstree [options] [pid or username]

Working with pstree command

Let us have a look at the pstree commands and their output to know their functions.

Option Description
pstree command line arguments for the processes are displayed
pstree -c expands identical subtrees in the output
pstree -g displays the process group IDs in the output
pstree -h highlights the current process along with its ancestors
pstree -l wraps long lines at the end of our terminal screen
pstree -n sort the processes by numeric sort(using PID) rather than by name
pstree -p displays pids for each process name
pstree -T hide threads in the output tree
pstree -u Show uid transitions. Whenever the uid of a process differs from the uid of its parent, the new uid is shown in parentheses after the process name
pstree -V displays the version information, use this command
pstree -Z displays the current security attributes of the process.
pstree | less displays the output one page at a time.

1. Display in a tree-like structure

Unlike the ps command which displays the running processes in a list structure, the pstree command displays the active processes in a tree-shaped structure.

pstree

The basic structure of the pstree command:

parent————child(1)————subchild (1)
        |          |--subchild (2)
        |
        |-child(2)
pstree command

2. How to display command-line arguments?

In order to display the command line arguments for certain processes, we need to use the pstree -a command to accomplish it. A process whose command line has been swapped out is displayed  in parentheses.

pstree -a
pstree -a command

3. How to display identical branches separately?

While using the pstree command, the default behaviour is to merge identical branches by putting them in square brackets and prefixing them with a repetition count. Using the below command you can view the subtrees expanded from the parent tree.

pstree -c
pstree -c command

It has displayed all the subtrees separately.

4. How to display process group IDs?

Use the “-g” command with pstree to display the process group IDs in the output window.

pstree -g
pstree -g command

5. How to highlight the curent process?

To highlight the current running process, its parents and sub branches, use the pstree -h command. It will display the current process along with its ancestors.

pstree -h
pstree -h command

6. How to wrap the output?

Usually the output gets cut at the edge of the terminal, and it will not give us a good view. Using “pstree -l” command makes sure that it wraps the whole output and gives us a full view of the output line.

pstree -l
pstree -l command

7. To sort the processes using PID

Adding -n next to pstree command, sorts the processes which are being displayed using the PID. By default, they are sorted using the name.

pstree -n
pstree -n command

8. To display PID for each process

If we want to see the PID for every process being displayed in the tree, use the pstree -p command. In the output, PIDs are included in parenthesis along with each process. We can also display PGIDs(Process group IDs) for each and every processes.

pstree -p
pstree -p command

9. How to hide the threads?

In order to hide the threads from the output tree and only show the processes, “pstree -T” command is used.

pstree -T
pstree -T command

10. To find the version information

If you want to display the version information, just type the command pstree -V.

pstree -V
pstree -V command

11. How to know the security attributes of the process?

To know the current security attributes of the process, use the command pstree -Z.

pstree -Z
pstree -Z command

Conclusion:

pstree being a visually appealing command is more useful than the ps command. To know more about the pstree options available, use the command man pstree which shows the available options in the pstree.


Monitor Your Entire Application with Atatus

Atatus is a Full Stack Observability Platform that lets you review problems as if they happened in your application. Instead of guessing why errors happen or asking users for screenshots and log dumps, Atatus lets you replay the session to quickly understand what went wrong.

We offer Application Performance Monitoring, Real User Monitoring, Server Monitoring, Logs Monitoring, Synthetic Monitoring, Uptime Monitoring and API Analytics. It works perfectly with any application, regardless of framework, and has plugins.

Atatus can be beneficial to your business, which provides a comprehensive view of your application, including how it works, where performance bottlenecks exist, which users are most impacted, and which errors break your code for your frontend, backend, and infrastructure.

If you are not yet a Atatus customer, you can sign up for a 14-day free trial .