Master the Linux ‘pstree’ Command: A Comprehensive Guide

Peter Hou
3 min readJun 5, 2023

--

This article provides a comprehensive guide to the 'pstree' command in Linux, covering its history, purpose, usage, and various parameters. With the help of common use cases and examples, it enables new users to understand and effectively use the 'pstree' command.

Instructions

This article is designed to provide readers with a detailed understanding of the 'pstree' command in Linux. It will discuss the history, use, and application of the 'pstree' command. It will also cover common and additional supported parameters, and offer some common use cases and tips for using the 'pstree' command effectively.

History

The 'pstree' command has been a part of Unix/Linux systems for many years. It is an invaluable tool for developers and system administrators who need to view the system's process tree.

When and why to use it

The 'pstree' command is used when there is a need to display the system's process tree. It visually displays the parent-child relationships between processes in a way that is easier to comprehend than other process viewing commands, such as 'ps'.

How to use it

The 'pstree' command can be run without any options, which will display the entire process tree of the system.

$ pstree
init─┬─NetworkManager───2*[{NetworkManager}]
├─acpid
├─avahi-daemon───avahi-daemon
├─cron
└─dbus-daemon

The commonly used parameters

  • -p shows PIDs. Besides the command names, PIDs will also be displayed.
$ pstree -p
init(1)─┬─NetworkManager(876)───2*[{NetworkManager}]
├─acpid(877)
├─avahi-daemon(904)───avahi-daemon(905)
├─cron(882)
└─dbus-daemon(874)
  • -u shows usernames of process owners.
$ pstree -u
init─┬─NetworkManager(root)───2*[{NetworkManager}]
├─acpid
├─avahi-daemon(avahi)───avahi-daemon
├─cron
└─dbus-daemon

Other supported parameters

  • -a : Shows command line arguments.
  • -c : Don't compact identical subtrees.
  • -g : Show PGIDs (process group IDs) and SID (session ID).
  • -l : Show long lines.
  • -n : Sort processes with the same ancestor by PID.
  • -Z : Show security context (SELinux).

Most common use cases

One of the most common use cases for 'pstree' is to find out the parent process of a specific process, and its subsequent child processes.

$ pstree -p $$
This will display the tree of processes that stem from the current process.

The tricky skills

The 'pstree' command can be combined with other commands to get more specific information. For instance, you can pipe the output of 'pstree' to 'grep' to find all processes associated with a specific program.

$ pstree | grep firefox
This will list all processes related to the firefox web browser.

What needs to be noted

It's important to note that while 'pstree' provides a quick way to view the process hierarchy, it may not show every detail of the system's processes. For more in-depth information, it may be necessary to use more advanced commands or tools.

Conclusion

Overall, 'pstree' is a versatile and powerful command that can greatly assist in monitoring and understanding the processes running on a Linux system. It's straightforward to use, and with a little practice, can become an essential part of any Linux user's toolkit.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet