In this article we will discuss about pstree command. On Linux machine, sometimes you need to check which services processes are currently running. You can find out information about the running processes by multiple ways using different commands.
The pstree
command is similar to ps
, but instead of listing the running processes, it shows them in a tree. It is a more convenient way to show the processes hierarchy.
Use the pstree Command
Below is the basic syntax for the pstree
command:
ps [OPTIONS] [USER or PID]
If you will run pstree
command without any options it will show the hierarchical tree structure of all running processes:
pstree
systemd─┬─accounts-daemon───2*[{accounts-daemon}]
├─2*[agetty]
├─atd
├─cron
├─dbus-daemon
├─do-agent───4*[{do-agent}]
├─fail2ban-server───2*[{fail2ban-server}]
├─lvmetad
├─lxcfs───10*[{lxcfs}]
├─master─┬─pickup
│ └─qmgr
...
In the output the systemd
is the root of the tree and it’s parent process of all processes. The 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.
Following is the example showing how the square brackets are used:
├─2*[agetty]
Above is similar to the below:
├─agetty ├─agetty
You can disable the merging the identical branches by using -c
option:
pstree -c
The parent process display the threads of a process using the process name inside curly braces. Below is an example:
├─lxcfs───10*[{lxcfs}]
You can use the -t
option to show the full threads names and to hide threads and show only processes use the -T
option.
Generally, the output of the pstree
is in multiple lines so output doesn’t fit on the screen. To view the output page wise use the less
command as following:
pstree | less
To view the processes of a particular user, you can pass user name as an argument with the pstree
command. For example, if you would like to show process of a user tecnstuff
, you would run command as following:
pstree tecnstuff
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 418
sshd───bash───pstree
By adding -s
option followed by the PID with pstree
command, it will show the parent process of the given process:
pstree -s 418
systemd───sshd───sshd───bash───pstree
Show PIDs and PGIDs
Sometimes, Linux user required to know the process id of a process to kill the malfunctioning the process.
Use the -p
option with pstree
to show the PIDs:
pstree -p
PIDs are shown in parentheses after each process or thread.
systemd(1)─┬─accounts-daemon(836)─┬─{accounts-daemon}(850)
│ └─{accounts-daemon}(857)
├─agetty(873)
├─agetty(876)
├─atd(838)
├─cron(831)
├─dbus-daemon(841)
├─do-agent(828)─┬─{do-agent}(852)
│ ├─{do-agent}(853)
│ ├─{do-agent}(854)
│ └─{do-agent}(31983)
├─fail2ban-server(866)─┬─{fail2ban-server}(1039)
│ └─{fail2ban-server}(1040)
The pstree
command sorts the processes by the parent name by default. To sort by PIDs (numeric) use -n
option along with pstree
command:
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.
├─lxcfs(833)─┬─{lxcfs}(833) │ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ ├─{lxcfs}(833)
│ └─{lxcfs}(833)
When merging is disabled the PIDs or PGIDs are displayed.
Highlighting
For better visual representation the pstree
command allows you to highlight processes.
Use the -h
option with 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 command throw an error and exit that means the highlighting is not supported.
Conclusion
You learned how to view running process in tree view using the pstree command. To know more about all available pstree options, type man pstree in your terminal.
If you have any questions or suggestions, please leave a comment below.