The Beginner’s Guide to pstree Command on Linux

pstree is a powerful and useful command for displaying running processes in Linux. Like its companion ps, it shows all running processes currently active on your logged-in system. The main difference is that the processes are organized in a tree instead of in a list. This tree shows processes in a parent-child relationship. The parent process is the spawning process, which creates all the child processes below it.

The structure pf pstree is similar to hierarchical directories on Unix systems like Linux and macOS. Using this structure you can quickly navigate through your process tree to discover what processes spawn or control each other. This allows for precise elimination of troublesome or out-of-control processes with the kill command.

To run the basic form of the command, open a Terminal window and type the following command, then press Enter:

pstree-guide-linux-1

This command alone will show a list of all running processes. The process at the very top (systemd in this case) is the parent process for everything running on your machine. Processes below it were spawned or open through systemd. Further layers of indentation indicate similar relationships, much like a family tree.

The basic structure of the pstree can be seen in the following iamage.

By default, processes with the same parent will be sorted alphabetically. There are other ways to sort in pstree using flags that we discuss below.

Like most Terminal programs, there’s more to pstree than just the single command. In addition to the basic functionality of pstree, different flags can trigger more complicated output from the program.

In order to expose process-identifying information, we can use the -p flags, which shows process-identification numbers, or PIDs.

pstree-guide-linux-2

We can also sort processes by PID instead of name with the -n flag.

pstree-guide-linux-3

You might notice that the output of pstree often gets cut off at the edge of your terminal window. You can manage that with the -l flag which “wraps” long lines at the edge of your terminal screen.

pstree-guide-linux-4

However, that might make the output a little hard to read, as you can see above.

You can also see only parts of the process tree. There are two ways to do that. The first is with the -s flag, which allows you to see the parent of the child process you specify. You can call out processes with its PID.

pstree-guide-linux-5

You might also want to see processes spawned by a current user. If you’re on a multi-user system, this can give you a good idea of who is doing what on your system. On a single user system, it’s not as informative. To see the processes spawned under a user, just type the user’s name after the main command.

pstree-guide-linux-6

That will show any commands executed under that user’s account, whether through programs the user runs or through commands they explicitly execute.

As always, each version of Linux might have a slight twist on this command, but it should be very standard on popular installations. To get help with your version, type man pstree in your command line and press Enter to open the command’s manual page, or “man page.” You can also view the basic man page for pstree.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.