pstree Command in Linux | Explained

In Linux, the user can list all the active processes using the “pstree” command. This command displays all the running processes of the current working system in a hierarchical or tree format. The tree format is beneficial for displaying the relationship between processes.

Considering its importance, this post explains the basics, working, and functionalities of the “pstree” command in Linux. The guideline of this post is as listed below:

What is the pstree Command in Linux?

As discussed earlier, the output of the pstree command is a tree-like structure that helps the users to navigate every single process quickly and easily. Let’s go through the syntax of the pstree command:

Syntax:

$ pstree [options] [pid or username]

The above syntax contains the following contents:

  • pstree: It is the main keyword in the syntax.
  • options: Essential options of the “pstree” command.
  • pid or username: The “pid” stands for “Process identification number”, which is a unique process id.

To get more information about “pstree”, run the below “help” command in the terminal “Ctrl+Alt+T”.

$ pstree --help

How to Use pstree Command in Linux?

Generally, the “pstree” command provides numerous options that allow the users to get the list of running processes according to the requirements. The following practical examples describe the working of the “pstree” command.

Example 1: Show the Hierarchy of All Running Processes

The most basic use of the “pstree” command can be observed in the following command:

$ pstree

All the running processes can be seen in the above screenshot in a tree-like structure.

Example 2: Display the Command Line Arguments

The “-a” option of the “pstree” command is beneficial for showing the commands and their arguments that are not displayed by default. To get these arguments, run the “pstree” command with the “-a” option:

$ pstree -a

The above output displays all the commands with their arguments or flags.

Example 3: Display All the Processes With PIDs

To get the “PIDs(Process IDs)” in the output, use the “-p” option of the “pstree” command as directed below:

$ pstree -p

The output has displayed all the process IDs in the terminal.

Example 4: Show the Specified User Processes

The “-u” argument of the “pstree” command helps to get the specified user process. Suppose in this example the currently logged-in user is “itslinuxfoss”. Type that user name with the “pstree” command and see the output:

$ pstree -u itslinuxfoss

The “pstree” command displays all the processes of the mentioned user “itslinuxfoss”.

Example 5: Show the Current Process

The “-h” options highlight the currently active process along its root and the subdirectories. Type the “pstree” command with the “-h” option for highlighting the current system active processes:

$ pstree -h

In the above output the currently active process is “systemd”.

Example 6: Show the Identical Subtrees

The “pstree” command does not display the identical subtrees by default. To list down these identical subtrees in the output, use the “-c” option of the “pstree” command:

$ pstree -c

The “pstree -c” command displays all the identical subtrees separately.

Example 7: List All the Processes in Numeric Order

The “-n” argument is useful for sorting all the processes in numeric order. Execute the “pstree” command with the “-n” option for sorting the current system running processes in numeric manners:

$ pstree -n

The above “pstree” output is now sorted numerically.

Example 8: Display the Organized pstree Output

Generally, the output of the “pstree” command cut the line that is too long in the output as shown in the screenshot:

$ pstree

But it can be wrapped using the “-l” option of the “pstree” command and displays into the next line:

$ pstree -l

That’s all about the working of the “pstree” command.

Conclusion

In Linux, the “pstree” command shows the currently running processes of the system in a tree structure. The tree structure or a parent-child relationship is useful for navigating and checking each process easily. This post has provided the working and usage of the “pstree” command in Linux.