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.
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.
How to Use the pstree
Command
The general syntax for the pstree
command is as follows:
ps [OPTIONS] [USER or PID]
In it’s simplest form when invoked without any option or argument, pstree
displays a hierarchical tree structure of all running processes:
pstree
systemd─┬─VBoxService───7*[{VBoxService}]
├─accounts-daemon───2*[{accounts-daemon}]
├─2*[agetty]
├─atd
├─cron
├─dbus-daemon
├─irqbalance───{irqbalance}
├─2*[iscsid]
├─lvmetad
├─lxcfs───2*[{lxcfs}]
├─networkd-dispat───{networkd-dispat}
├─nginx───2*[nginx]
...
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 “testuser”:
pstree testuser
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
systemd───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)─┬─VBoxService(955)─┬─{VBoxService}(956)
│ ├─{VBoxService}(957)
│ ├─{VBoxService}(958)
│ ├─{VBoxService}(959)
│ ├─{VBoxService}(960)
│ ├─{VBoxService}(961)
│ └─{VBoxService}(962)
...
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.