[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 14, 2023

How to use pstree command.

How To Use Pstree Command.

In this article, we will talk about the pstree command. It is similar to ps , but instead of listing the running processes, it shows them in a tree. The tree-like format is a more convenient way to display the processes hierarchy and makes the output more visually appealing.

When working on a Linux machine, sometimes you might need to find out what processes are currently running. There are number of commands that you can use to find information about the running processes, with ps and top being the most commonly used ones.

 

How to Use the pstree Command

The general syntax for the pstree command is as follows:

ps [OPTIONS] [USER or PID]

If you encounter the below error while running the pstree command:

pstree: command not found

you may try installing the package as shown below as per your choice of distribution.

Distribution

Command

OS X

brew install pstree

Debian

apt-get install psmisc

Ubuntu

apt-get install psmisc

Alpine

apk add psmisc

Arch Linux

pacman -S psmisc

Kali Linux

apt-get install psmisc

CentOS

yum install psmisc

Fedora

dnf install psmisc

Raspbian

apt-get install psmisc

In it’s simplest form when invoked without any option or argument, pstree displays a hierarchical tree structure of all running processes:

pstree
systemd─┬─agetty
        ├─atd
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─irqbalance
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───bash───pstree
        │      └─sshd───sshd
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─varnishd───varnishd───216*[{varnishd}]

The top/root item in the tree is the parent process of all system processes. In this example that is systemd, which is the first process that start on boot.

pstree merges identical branches by putting them between square brackets and prefixing them with an integer that represents the number of branches. This makes the output more readable and visually appealing.

Below is an example showing how the square brackets are used:

├─2*[agetty]

is same as:

├─agetty        
├─agetty

To disable the merging of the identical branches, use the -c option:

pstree -c

The threads of a process are shown under the parent process and displayed using the process name inside curly braces. Here is an example:

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

Use the -t option to show the full threads names. If you want to hide threads and show only processes use the -T option.

Typically, pstree displays many lines of output that doesn’t fit on the screen. To view the output one page at a time, pipe it to the less command:

pstree | less

If a user name is given as an argument, pstree shows only the processes owned by that user. For example, the following command will show only those processes that have been started by a user named “linuxize”:

pstree root

When PID is specified as an argument, pstree displays a tree with the given process as the root of the tree. Here is an example:

pstree 1943
sshd───bash───pstree

To show the parent processes of the given process use the -s option followed by the process PID:

pstree -s 1943
├─sshd─┬─sshd───bash───pstree

 

Show PIDs and PGIDs

Usually, when running the pstree command, the most important information the user is looking for is the process ID. For example, knowing the PID allows you to kill a malfunctioning process .

The -p option instructs pstree to show the PIDs:

pstree -p

PIDs are shown in parentheses after each process or thread.

systemd(1)─┬─agetty(587)
           ├─atd(578)
           ├─auditd(528)───{auditd}(529)
           ├─chronyd(566)
           ├─crond(577)
           ├─dbus-daemon(560)───{dbus-daemon}(572)
           ├─firewalld(593)───{firewalld}(830)
           ├─irqbalance(557)
           ├─master(1290)─┬─pickup(28946)
           │              └─qmgr(1307)

By default, pstree, sort processes with the same parent by name. The -n option tells pstree to use numeric sort, i.e. sort by PIDs:

pstree -pn

The process group ID or PGIDs is the process ID of the first member of the process group. To view PGIDs use the -g option:

pstree -g

PIDs are also shown in parentheses after each process or thread.

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

When PIDs or PGIDs are shown, merging is implicitly disabled.

 

Show Command Line Arguments

Some programs can be invoked with configuration options specified as command-line arguments.

By default, the pstree does not show you the command line arguments for the running processes. To view how the process was started, use the command together with the -a option:

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

 

Highlighting

pstree also allows you to highlight processes for better visual representation.

The -h option instructs pstree to highlight the current process and all its ancestors.

pstree -h

To highlight a specific process, use the -H option followed by the process ID:

pstree -H PID_NUMBER

If the highlighting is not supported, the command will exit with an error.

 

Conclusion

The pstree command displays the running processes in the form of a tree structure.

For information about all available pstree options, type man pstree in your terminal.

Keep reading
What is error 400?
August 14, 2023

What is error 400?

What is 400 error code? The HTTP 400 status code, also known as “400 Bad Request,” is a client-side error response code that indicates the server cannot process the request due to a problem with the request itself. “400 Bad Request” is a general error that indicates that your browser sends a…

Read article
What is 401 error code?
August 14, 2023

What is 401 error code?

What is 401 error code? The 401 Error, also known as the 401 Unauthorised Error, is one of the most common error codes. This error indicates that the request to the website or web application was unable to be authenticated. Fortunately, in most cases, this error is easily corrected. The 401 Error…

Read article
What Is 403 Forbidden Error?
August 14, 2023

What Is 403 Forbidden Error?

What Is 403 Forbidden Error? In this article, we will explain what a 403 forbidden error is, as well as we will let you know 3 methods to fix it. What Is a 403 Forbidden Error? The 403 Forbidden Error is an HTTP status code that pops up when you try to go to a…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00