Oct 15, 2023 4 min read

Pstree Command in Linux

Discuss the pstree command in linux with our step-by-step tutorial. It is used to display a tree-like structure of processes on the system

Pstree Command in Linux
Table of Contents

Introduction

Before we start discussing about pstree command in linux, let's first understand-What is a Pstree Command ?

The pstree command in Linux is used to display a tree-like representation of processes on the system. It visually presents the hierarchical relationship between processes, helping you understand the parent-child relationship of running processes.

When working on a Linux machine, you may need to know what processes are operating at any given time. There are several commands that may be used to obtain information about running processes, the most often used being ps and top.

The pstree command will be discussed in this tutorial.

How to Use the pstree Command

The pstree command has the following general syntax:

ps [OPTIONS] [USER or PID]

In its most basic version, pstree displays a hierarchical tree structure of all running processes when executed without any options or arguments:

pstree
Output

systemd─┬─VBoxService───7*[{VBoxService}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─2*[agetty]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─irqbalance───{irqbalance}
        ├─2*[iscsid]
        ├─lvmetad
        ├─lxcfs───2*[{lxcfs}]
        ├─networkd-dispat───{networkd-dispat}
        ├─nginx───2*[nginx]
...

The parent process of all system processes is the top/root item in the tree. In this case, it's systemd, which is the first process to run when the computer boots up.

By inserting identical branches within square brackets and prefixing them with an integer that reflects the number of branches, pstree combine them. This improves the readability and aesthetic attractiveness of the output.

The following is an example of how square brackets are used:

Output

├─2*[agetty]

is similar to:

├─agetty        
├─agetty

Use the -c option to prevent the merging of identical branches:

pstree -c

The threads of a process are presented using the process name inside curly brackets under the parent process. Here's an illustration:

Output

├─lxcfs───2*[{lxcfs}]

To see the entire thread names, use the -t option. The -T option is used to hide threads and only show processes.

pstree usually outputs a large number of lines that don't fit on the screen. Pipe the output to the less command to read it one page at a time:

pstree | less

When a user name is specified as an argument, pstree only displays processes that belong to that user. For instance, the following command will display only those processes launched by the user "vega":

pstree vega

Pstree displays a tree with the supplied process as the root when PID is specified as an argument. Here's an example:

pstree 1943
Output

sshd───bash───pstree

Use the -s option followed by the process PID to display the parent processes of a given process:

pstree -s 1943
systemd───sshd───sshd───bash───pstree

Show PIDs and PGIDs

When using the pstree command, the process ID is usually the most significant piece of information the user is looking for. Knowing the PID, for example, allows you to terminate a faulty process.

The -p option tells pstree to display PIDs:

pstree -p

After each process or thread, the PID is shown in parentheses.

Output

systemd(1)─┬─VBoxService(955)─┬─{VBoxService}(956)
           │                  ├─{VBoxService}(957)
           │                  ├─{VBoxService}(958)
           │                  ├─{VBoxService}(959)
           │                  ├─{VBoxService}(960)
           │                  ├─{VBoxService}(961)
           │                  └─{VBoxService}(962)
...```

pstree sorts processes with the same parent by name. The -n option instructs pstree to sort by PIDs using numeric sort:

pstree -pn

The process group ID, or PGIDs, is the process ID of the process group's initial member. Use the -g option to see PGIDs:

pstree -g

After each process or thread, PIDs are shown in parenthesis.

Output

systemd(1)─┬─VBoxService(954)─┬─{VBoxService}(954)
           │                  ├─{VBoxService}(954)
           │                  ├─{VBoxService}(954)
           │                  ├─{VBoxService}(954)
           │                  ├─{VBoxService}(954)
           │                  ├─{VBoxService}(954)
           │                  └─{VBoxService}(954)
...

Merging is disallowed while PIDs or PGIDs are displayed.

Show Command Line Arguments


Some programs can be started using command-line arguments specifying configuration choices.

The command-line arguments for ongoing processes are not displayed by default in the pstree. Use the command with the -a option to see how the process was started:

pstree -a
Output

...
  ├─agetty -o -p -- \\u --keep-baud 115200,38400,9600 ttyS0 vt220
  ├─agetty -o -p -- \\u --noclear tty1 linux
...

Highlighting

For a better visual representation, pstree additionally allows you to highlight processes.

The -h option tells pstree to highlight the current process as well as all of its forerunners.

pstree -h

Use the -H option followed by the process ID to highlight a specific process:

pstree -H PID_NUMBER

The command will exit with an error if the highlighting is not supported.

FAQs on Pstree Command in Linux

What is the purpose of the pstree command in Linux?

The pstree command displays the process tree, showing the relationship between processes on the system.

How do I use the pstree command to display the process tree?

Simply run the pstree command without any arguments, and it will display the process tree, starting from the current shell's process.

What information does pstree display for each process?

The pstree command displays the names of processes and their relationship in a tree-like format. The parent processes are organized above their child processes.

Can I customize the output of pstree with options?

Yes, the pstree command provides various options to customize the output. You can use options like -a to display command-line arguments, -h to highlight the current process, and more.

What does the -p option do in the pstree command?

The -p option in the pstree command displays the process tree along with the process IDs (PIDs) of each process.

Can I display the process tree for a specific process using pstree?

Yes, you can specify a PID as an argument to the pstree command to display the process tree for that specific process. For example, pstree 1234 will display the process tree for the process with PID 1234.

Is there a way to limit the depth of the process tree displayed by pstree?

Yes, you can use the -l option followed by a number to limit the depth of the process tree displayed by pstree. For example, pstree -l 3 will display a process tree up to a depth of 3 levels.

Conclusion

The pstree command shows the currently active processes as a tree structure.

In your terminal, type man pstree to get a list of all available pstree options.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.