In this article, we’ll be talking about the pstree command in Linux. When it comes to finding running processes on your Linux system, there are multiple options available. The ps command is the more popular option among these options. The top command is another popular choice for viewing the currently running processes. Both of these commands (ps and top) list the currently running processes. The pstree command is similar to these two, the only difference is that the pstree command displays processes in the form of a tree.
In this tutorial, we will learn about the pstree command and see how to use the different options available along with it.
How to use the pstree command?
To use pstree just enter the command and hit enter.
pstree
Output:
The tree shows processes in a hierarchical manner. ‘systemd‘ is the parent process for all other system processes. pstree visually merges identical branches by putting them in square brackets and prefixing them with the repetition count.
For example :
udisksd─+─{udisksd}
|-{udisksd}
|-{udisksd}
|-{udisksd}
Becomes :
udisksd───4*[{udisksd}]
You can also display the output without merging the identical branches together.
How to display identical branches separately in the tree?
To display the identical branches separately use the -c flag along with the pstree command.
pstree -c
Output :
Compare this with the output above and note the difference.
How to display PIDs for processes in the tree?
To display PIDs along with the processes in the output use :
pstree -p
Output :
PIDs are included in parenthesis along with each process in the output. Similarly, you can also display Process group IDs (PGIDs) for each process.
How to display PGIDs for processes in the tree?
To display PGIDs along with each process use :
pstree -g
Output:
How to display command-line arguments for the processes in the tree?
Pstree can also display the command line arguments to use for running the processes. To display the command-line arguments use -a flag along with the pstree command.
pstree -a
Output :
The output now also contains the command-line arguments.
How to display a particular process by its PIDs?
To display a particular process just input pstree followed by the PID. You can check the PID using the ps command. For example:
pstree 1131
Output :
{at-spi-bus-laun}
How to show parent processes of the specified process?
To show the parent processes of the process with a particular PID use the -s flag along with the pstree command. For example :
pstree -s 1131
Output :
systemd───systemd───at-spi-bus-laun───{at-spi-bus-laun}
How to highlight a particular process in the tree?
To highlight a particular process in the process tree use the -H flag along with the pstree command.
pstree -H 1131
Output :
How to hide threads in the process tree?
To hide threads in the tree use -T flag along with pstree.
pstree -T
Output :
Conclusion
This tutorial was about the pstree command in Linux. We learned how this command is used for displaying process in the shape of a tree. We also learn about the different options available along with the pstree command.