How to Use pstree to Display Running Linux Processes as a Tree

https://static1.makeuseofimages.com/wordpress/wp-content/uploads/2023/07/tree-branches.jpg

If you use Linux, you’ve most likely used ps, top, or htop to view currently running processes on the system. While these programs are useful, they don’t show the relationship of processes to each other.

pstree is a program that visualizes Linux processes as a tree. Here’s how you can install and use it on Linux.

What Is pstree?

pstree is a program that shows the processes as a tree-like structure. The parent processes are the “trunk” and the child processes are branches.

Using pstree for the first time reveals how Linux processes work. All Linux processes have the init process, which is systemd on most major mainstream distros, as their common ancestor.

How to Install pstree on Linux

pstree is such a useful utility that there’s a good chance it’s already installed on your system. If you try to run it and it isn’t, it’s available through your package manager. It’s part of the psmisc suite of utilities.

To install it on Debian and Ubuntu, type:

 sudo apt install psmisc 

To install pstree on Arch Linux:

 sudo pacman -S psmisc 

And on the Red Hat family of distros:

 sudo dnf install psmisc 

Use pstree to View Linux Processes as a Tree

Running pstree is simple. Just type pstree at the command line. You’ll see a tree of all the processes running on your system.

Supplying a username as an argument will show all the processes that the user owns. For example, to see a tree of the superuser’s processes:

 pstree root 

As with many other Linux programs, pstree has command-line options. You can see the PIDs of the processes with the -p option:

 pstree -p 

To highlight the current process, that is, your current shell, use the -h option.

 pstree -h 

You can highlight a certain PID with the -H (capital H) option, followed by the PID number:

 pstree -H [PID] 

You can get a color-coded representation of the age of the processes with the -C option, followed by the age value. This is currently the only argument that this option takes.

Processes spawned in the last 60 seconds will be displayed in green. Similarly, yellow represents processes that were started an hour ago. The remaining processes will be in red.

 pstree -C age 

pstree gives you a hierarchical tree-like view of your running Linux processes, true to its name. You can see how your processes flow out of the init process, and you can customize your view with different options.

Linux’s procfs also lets you see information on Linux processes right from the file system.

MakeUseOf