pstree Command in Linux



The pstree command in Linux displays the running processes in a tree-like structure. It organizes processes hierarchically based on their parent-child relationships, starting from the root of a specified process. The tree is rooted at the specified PID or defaults to init if none is provided. For a username, all process trees owned by that user are displayed. Identical branches are merged, enclosed in square brackets, and prefixed with a repetition count.

Table of Contents

Here is a comprehensive guide to the options available with the pstree command in Linux −

Syntax of pstree Command

The syntax of the pstree command in Linux is as follows −

pstree [options] [pid | username]

In the above syntax, the [options] field is used to specify various options to modify the output. The [pid | username] field is used to specify the process ID or username.

pstree Command Options

The options of the Linux pstree command are listed below −

Flags Options Description
-a --arguments Show command-line arguments. Swapped-out commands are in parentheses. Disables process compaction but not thread compaction.
-A --ascii Use ASCII characters to draw the tree.
-c --compact-not Disable compaction of identical subtrees. By default, subtrees are compacted whenever possible.
-C Type --color=Type Color process names by attributes (valid type is age). Newer processes are green, older ones yellow, and the oldest red.
-g --show-pgids Show PGIDs (Process Group IDs) as decimal numbers in parentheses after each process name.
-G --vt-100 Use VT100 line-drawing characters.
-h --highlight-all Highlight the current process and its ancestors, if the terminal supports highlighting.
-H PID --highlight-pid=PID Highlight a specified process instead of the current one. Fails if highlighting isn't supported.
-l --long Display long lines. Prevents truncation to screen width or default column size.
-n --numeric-sort Sort processes under the same parent numerically by PID instead of alphabetically by name.
-N TYPE --ns-sort=TYPE Show individual trees for each specified namespace (e.g., ipc, pid, user). Output is limited for non-root users.
-p --show-pids Show PIDs as decimal numbers in parentheses after process names. Disables compaction.
-s --show-parents Show parent processes of the specified process.
-S --ns-changes Show namespace transitions. Output is limited for non-root users.
-t --thread-names Show full names for threads when available.
-T --hide-threads Hide threads and display only processes.
-u --uid-changes Show UID transitions. Displays the UID in parentheses if it differs from the parent's UID.
-U --unicode Use UTF-8 (Unicode) line-drawing characters.
-V --version Display version information.
-Z --security-context Show current security attributes of processes, such as SELinux security context.

Examples of pstree Command in Linux

In this section, the usage of the pstree command in Linux will be discussed with examples −

Displaying the Entire Process Tree

To display the tree of all the processes currently running, use the pstree command without any option −

pstree
pstree Command in Linux1

The tree will be rooted at the init process or its equivalent.

Displaying Process Tree with Color

To display the tree of all the processes currently running with color, use the pstree with -C or --color with type option. Note that currently the pstree supports the value age for coloring, where processes newer than 60 seconds are green, those under an hour are yellow, and older processes are red.

pstree -C age
pstree Command in Linux2

Displaying PIDs in the Process Tree

To display the tree of all the processes currently running along with their PIDs, use the -p or --show-pids option −

pstree -p
pstree Command in Linux3

The PIDs are shown in parentheses after each process name.

Displaying the Command-Line Arguments

To display a process tree with the command-line arguments, use the -a or --arguments option with the pstree command −

pstree -a
pstree Command in Linux4

The command-line arguments refer to the parameters that are passed when a process is started.

Highlighting the Current Process

To highlight the current process and its ancestors in the tree, use the -h or --highlight-all option −

pstree -h

Note that this option has no effect if the terminal does not support highlighting or if the current process and its ancestors are not part of the displayed subtree.

Displaying Parent Process of a Process

To display the parent process of a process, use the -s or --show-parents option with the PID −

pstree -s 1028
pstree Command in Linux5

Sorting the Processes in the Tree

To sort the child processes under the same parent numerically, use the -n or --numeric-sort option with the pstree command −

pstree -n

Displaying Process Tree by Namespace Type

To show individual process trees for the specified PID namespace, use the -N or --ns-sort option. Namespace types are cgroup, ipc, mnt, net, pid, time, user, uts. For example, to sort by cgroup namespace type, use the pstree command in the following way −

pstree -N cgroup
pstree Command in Linux6

Displaying Process Tree with ASCII Characters

To display the tree using plain ASCII characters instead of line-drawing characters, use the -A or --ascii option −

pstree -A
pstree Command in Linux7

Displaying Process Tree Hiding the Threads

To display only processes, hiding individual threads within processes, use the -T or --hide-threads option −

pstree -T

Displaying the Security Context

For systems with SELinux, the -Z or --security-context option shows the security context of each process.

pstree -Z

Disabling the Tree Compaction

By default, subtrees are compacted whenever possible. To disable it, use the -c or --no-compact option −

pstree -c

Conclusion

The pstree command in Linux provides a tree-like visualization of running processes, showing parent-child relationships. It starts from a specified process ID (PID) or defaults to the root process (init). Options enhance the output, such as displaying PIDs, arguments, or highlighting specific processes. Features include using ASCII or Unicode for tree formatting, showing namespace-specific trees, and customizing colors based on process attributes.

Advertisements