pstree Command in Linux with Examples
Last Updated :
09 Oct, 2024
The pstree command in Linux is a powerful tool that displays running processes as a tree structure. This visual representation makes it easier to understand the hierarchy of processes, providing a more intuitive view of how processes are related to each other. The root of the tree is either the `init` process or a process with a specified PID. While pstree comes pre-installed on most Linux distributions, it can also be installed on other Unix-like systems.
Syntax
pstree [options] [pid or username]
Basic Pstree Example
To display the basic process tree, simply run the pstree command without any options:
Output:
This output shows the hierarchical relationship between processes, with child processes indented under their parent processes.
Key Options of the pstree Command
Option | Description |
---|
-a | Show command line arguments |
---|
-p | Show PIDs |
---|
-c | Disable compaction of identical subtrees |
---|
-n | Sort processes with the same parent by PID |
---|
-u | Show uid transitions |
---|
-h | Highlight current process and its ancestors |
---|
-g | Show PGID numbers |
---|
-V | Display version information |
---|
1. Display Command Line Arguments (-a)
To include command line arguments in the output:
-a optionOutput:
outputThis output shows more detailed information about each process, including the command line arguments used to start them.
2. Display PIDs (-p)
To show Process IDs (PIDs) for each process:
-p option3. Disable Compaction of Identical Subtrees (-c)
By default, `pstree` compacts identical branches. Use the `-c` option to expand all subtrees:
Disable Compaction of Identical Subtrees4. Sort Processes by PID (-n)
To sort processes with the same parent by PID instead of by name:
Sort Processes by PID5. Show User/Owner of Processes (-u)
To display the owner of each process:
Show User/Owner of Processes6. Highlight Current Process (-h)
To highlight the current process and its ancestors:
Highlight Current Process7. Show Process Group IDs (-g)
To display process group IDs:
Show Process Group IDsAdvanced Usages of pstree Command
1. Display Process Tree for a Specific User
To show all process trees rooted at processes owned by a specific user:
Display Process Tree for a Specific UserReplace `username` with the actual username you want to investigate.
2. Display Version Information
To check the version of `pstree` installed on your system:
Display Version InformationOutput:
OutputConclusion
The pstree command is a versatile tool for visualizing and analyzing the process hierarchy in Linux systems. Its various options allow users to customize the output to suit their needs, whether it's identifying process relationships, troubleshooting, or system monitoring. By mastering `pstree`, system administrators and power users can gain valuable insights into their system's process structure and behavior.