Linux pstree Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux pstree command, which is a useful tool for visualizing the process hierarchy on a Linux system. The pstree command displays a tree-like representation of the running processes, showing the parent-child relationships between them. This can be helpful for understanding the structure and dependencies of your system's processes. We will start by understanding the pstree command, then explore the process hierarchy using pstree, and finally learn how to filter and customize the pstree output.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicSystemCommandsGroup -.-> linux/tree("`Directory Tree Display`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") subgraph Lab Skills linux/tree -.-> lab-422868{{"`Linux pstree Command with Practical Examples`"}} linux/grep -.-> lab-422868{{"`Linux pstree Command with Practical Examples`"}} linux/ps -.-> lab-422868{{"`Linux pstree Command with Practical Examples`"}} end

Understand the pstree Command

In this step, we will learn about the pstree command, which is a useful tool for visualizing the process hierarchy on a Linux system.

The pstree command displays a tree-like representation of the running processes, showing the parent-child relationships between them. This can be helpful for understanding the structure and dependencies of your system's processes.

To start, let's run the pstree command in the terminal:

$ pstree
systemd─┬─ModemManager─┬─dhclient
        │             └─2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─apache2─┬─apache2
        │         └─2*[apache2]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │         │            │                └─3*[{containerd-shim}]
        │         │            └─6*[{containerd}]
        │         └─7*[{dockerd}]
        ├─gdm3─┬─Xorg
        │      └─gnome-session-b─┬─gnome-shell
        │                        ├─ibus-daemon─┬─ibus-engine-sim
        │                        │             └─2*[{ibus-daemon}]
        │                        └─2*[{gnome-session-b}]
        ├─irqbalance
        ├─kerneloops
        ├─kube-proxy
        ├─kubelet
        ├─lightdm─┬─Xorg
        │         └─lightdm───sh───fluxbox
        ├─lxcfs
        ├─lxd─┬─lxd
        │     └─2*[{lxd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd
        ├─snapd───10*[{snapd}]
        ├─ssh-agent
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-networkd
        ├─systemd-resolve
        ├─systemd-timesyn
        ├─systemd-udevd
        ├─udisksd───3*[{udisksd}]
        ├─unattended-upgr───2*[{unattended-upgr}]
        ├─upowerd───2*[{upowerd}]
        ├─user-manager───3*[{user-manager}]
        └─whoopsie───2*[{whoopsie}]

The output shows a hierarchical view of the running processes, with the systemd process at the top as the parent of many other processes.

You can customize the output of pstree using various options. For example, to show only the process IDs (PIDs) instead of the process names, you can use the -p option:

$ pstree -p
systemd(1)─┬─ModemManager(577)─┬─dhclient(593)
          │                   └─2*[{ModemManager}(580,581)]
          ├─NetworkManager(572)─┬─dhclient(590)
          │                    └─2*[{NetworkManager}(573,574)]
          ├─accounts-daemon(531)───2*[{accounts-daemon}(532,533)]
          ├─apache2(688)─┬─apache2(689)
          │             └─2*[apache2(690,691)]
          ├─atd(544)
          ├─cron(545)
          ├─dbus-daemon(530)
          ├─dockerd(688)─┬─containerd(701)─┬─containerd-shim(702)─┬─bash(703)
          │             │                 │                       └─3*[{containerd-shim}(704,705,706)]
          │             │                 └─6*[{containerd}(707,708,709,710,711,712)]
          │             └─7*[{dockerd}(689,690,691,692,693,694,695)]
          ├─gdm3(575)─┬─Xorg(586)
          │           └─gnome-session-b(587)─┬─gnome-shell(588)
          │                                 ├─ibus-daemon(589)─┬─ibus-engine-sim(592)
          │                                 │                  └─2*[{ibus-daemon}(590,591)]
          │                                 └─2*[{gnome-session-b}(595,596)]
          ├─irqbalance(543)
          ├─kerneloops(546)
          ├─kube-proxy(697)
          ├─kubelet(696)
          ├─lightdm(576)─┬─Xorg(585)
          │              └─lightdm(577)───sh(578)───fluxbox(579)
          ├─lxcfs(542)
          ├─lxd(698)─┬─lxd(699)
          │         └─2*[{lxd}(700,701)]
          ├─polkitd(534)───2*[{polkitd}(535,536)]
          ├─rsyslogd(541)
          ├─snapd(694)───10*[{snapd}(695,696,697,698,699,700,701,702,703,704)]
          ├─ssh-agent(587)
          ├─systemd-journal(526)
          ├─systemd-logind(529)
          ├─systemd-networkd(527)
          ├─systemd-resolve(528)
          ├─systemd-timesyn(540)
          ├─systemd-udevd(525)
          ├─udisksd(537)───3*[{udisksd}(538,539,540)]
          ├─unattended-upgr(547)───2*[{unattended-upgr}(548,549)]
          ├─upowerd(535)───2*[{upowerd}(536,537)]
          ├─user-manager(586)───3*[{user-manager}(587,588,589)]
          └─whoopsie(550)───2*[{whoopsie}(551,552)]

This shows the process IDs instead of the process names.

You can also use the -u option to display the user running each process:

$ pstree -u
systemd─┬─ModemManager─┬─dhclient
        │             └─2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─apache2─┬─apache2
        │         └─2*[apache2]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │         │            │                └─3*[{containerd-shim}]
        │         │            └─6*[{containerd}]
        │         └─7*[{dockerd}]
        ├─gdm3─┬─Xorg
        │      └─gnome-session-b─┬─gnome-shell
        │                        ├─ibus-daemon─┬─ibus-engine-sim
        │                        │             └─2*[{ibus-daemon}]
        │                        └─2*[{gnome-session-b}]
        ├─irqbalance
        ├─kerneloops
        ├─kube-proxy
        ├─kubelet
        ├─lightdm─┬─Xorg
        │         └─lightdm───sh───fluxbox
        ├─lxcfs
        ├─lxd─┬─lxd
        │     └─2*[{lxd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd
        ├─snapd───10*[{snapd}]
        ├─ssh-agent
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-networkd
        ├─systemd-resolve
        ├─systemd-timesyn
        ├─systemd-udevd
        ├─udisksd───3*[{udisksd}]
        ├─unattended-upgr───2*[{unattended-upgr}]
        ├─upowerd───2*[{upowerd}]
        ├─user-manager───3*[{user-manager}]
        └─whoopsie───2*[{whoopsie}]

This shows the user running each process.

The pstree command can be a valuable tool for understanding the process hierarchy on your Linux system. In the next step, we'll explore more ways to use pstree to gain insights into your system's processes.

Explore the Process Hierarchy with pstree

In this step, we will dive deeper into the process hierarchy using the pstree command and explore various options to customize the output.

First, let's see how we can display the process hierarchy in a more compact way using the -c option:

$ pstree -c
systemd─┬─ModemManager─┬─dhclient
        │             └─2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─apache2─┬─apache2
        │         └─2*[apache2]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │         │            │                └─3*[{containerd-shim}]
        │         │            └─6*[{containerd}]
        │         └─7*[{dockerd}]
        ├─gdm3─┬─Xorg
        │      └─gnome-session-b─┬─gnome-shell
        │                        ├─ibus-daemon─┬─ibus-engine-sim
        │                        │             └─2*[{ibus-daemon}]
        │                        └─2*[{gnome-session-b}]
        ├─irqbalance
        ├─kerneloops
        ├─kube-proxy
        ├─kubelet
        ├─lightdm─┬─Xorg
        │         └─lightdm───sh───fluxbox
        ├─lxcfs
        ├─lxd─┬─lxd
        │     └─2*[{lxd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd
        ├─snapd───10*[{snapd}]
        ├─ssh-agent
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-networkd
        ├─systemd-resolve
        ├─systemd-timesyn
        ├─systemd-udevd
        ├─udisksd───3*[{udisksd}]
        ├─unattended-upgr───2*[{unattended-upgr}]
        ├─upowerd───2*[{upowerd}]
        ├─user-manager───3*[{user-manager}]
        └─whoopsie───2*[{whoopsie}]

The -c option compacts the output, making it easier to read and understand the process hierarchy.

Next, let's see how we can filter the output to focus on specific processes. For example, to show only the processes related to the docker service, we can use the -a option:

$ pstree -a | grep docker
dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │            │                └─3*[containerd-shim]
        │            └─6*[containerd]
        └─7*[dockerd]

This shows the docker processes and their child processes.

You can also use the -p option to display the process IDs (PIDs) instead of the process names:

$ pstree -p | grep docker
dockerd(688)─┬─containerd(701)─┬─containerd-shim(702)─┬─bash(703)
             │                 │                     └─3*[containerd-shim(704,705,706)]
             │                 └─6*[containerd(707,708,709,710,711,712)]
             └─7*[dockerd(689,690,691,692,693,694,695)]

This can be useful when you need to identify specific processes by their PIDs.

Finally, let's see how we can display the process hierarchy for a specific user. To do this, we can use the -u option followed by the username:

$ pstree -u labex
systemd─┬─ModemManager─┬─dhclient
        │             └─2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─apache2─┬─apache2
        │         └─2*[apache2]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │         │            │                └─3*[{containerd-shim}]
        │         │            └─6*[{containerd}]
        │         └─7*[{dockerd}]
        ├─gdm3─┬─Xorg
        │      └─gnome-session-b─┬─gnome-shell
        │                        ├─ibus-daemon─┬─ibus-engine-sim
        │                        │             └─2*[{ibus-daemon}]
        │                        └─2*[{gnome-session-b}]
        ├─irqbalance
        ├─kerneloops
        ├─kube-proxy
        ├─kubelet
        ├─lightdm─┬─Xorg
        │         └─lightdm───sh───fluxbox
        ├─lxcfs
        ├─lxd─┬─lxd
        │     └─2*[{lxd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd
        ├─snapd───10*[{snapd}]
        ├─ssh-agent
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-networkd
        ├─systemd-resolve
        ├─systemd-timesyn
        ├─systemd-udevd
        ├─udisksd───3*[{udisksd}]
        ├─unattended-upgr───2*[{unattended-upgr}]
        ├─upowerd───2*[{upowerd}]
        ├─user-manager───3*[{user-manager}]
        └─whoopsie───2*[{whoopsie}]

This shows the process hierarchy for the labex user.

The pstree command provides a wealth of information about the process hierarchy on your Linux system. By using the various options, you can customize the output to suit your needs and gain a deeper understanding of your system's processes.

Filter and Customize the pstree Output

In this final step, we will explore more ways to filter and customize the output of the pstree command to suit your specific needs.

One common use case is to focus on a specific process or process group. For example, let's say you want to see the process hierarchy for the systemd process and its child processes. You can use the -s option to do this:

$ pstree -s systemd
systemd

This shows the systemd process and its immediate child processes.

You can also use the -g option to group processes by their process group ID (PGID) instead of the process hierarchy:

$ pstree -g
1-systemd
  ├─577-ModemManager
  │   ├─593-dhclient
  │   └─2*[580-{ModemManager},581-{ModemManager}]
  ├─572-NetworkManager
  │   ├─590-dhclient
  │   └─2*[573-{NetworkManager},574-{NetworkManager}]
  ├─531-accounts-daemon
  │   ├─532-{accounts-daemon}
  │   └─533-{accounts-daemon}
  ├─688-apache2
  │   ├─689-apache2
  │   └─2*[690-apache2,691-apache2]
  ├─544-atd
  ├─545-cron
  ├─530-dbus-daemon
  ├─688-dockerd
  │   ├─701-containerd
  │   │   ├─702-containerd-shim
  │   │   │   └─703-bash
  │   │   └─6*[707-{containerd},708-{containerd},709-{containerd},710-{containerd},711-{containerd},712-{containerd}]
  │   └─7*[689-{dockerd},690-{dockerd},691-{dockerd},692-{dockerd},693-{dockerd},694-{dockerd},695-{dockerd}]
  ├─575-gdm3
  │   ├─586-Xorg
  │   └─587-gnome-session-b
  │       ├─588-gnome-shell
  │       ├─589-ibus-daemon
  │       │   ├─592-ibus-engine-sim
  │       │   └─2*[590-{ibus-daemon},591-{ibus-daemon}]
  │       └─2*[595-{gnome-session-b},596-{gnome-session-b}]
  ├─543-irqbalance
  ├─546-kerneloops
  ├─697-kube-proxy
  ├─696-kubelet
  ├─576-lightdm
  │   ├─585-Xorg
  │   └─577-lightdm
  │       ├─578-sh
  │       └─579-fluxbox
  ├─542-lxcfs
  ├─698-lxd
  │   ├─699-lxd
  │   └─2*[700-{lxd},701-{lxd}]
  ├─534-polkitd
  │   ├─535-{polkitd}
  │   └─536-{polkitd}
  ├─541-rsyslogd
  ├─694-snapd
  │   └─10*[695-{snapd},696-{snapd},697-{snapd},698-{snapd},699-{snapd},700-{snapd},701-{snapd},702-{snapd},703-{snapd},704-{snapd}]
  ├─587-ssh-agent
  ├─526-systemd-journal
  ├─529-systemd-logind
  ├─527-systemd-networkd
  ├─528-systemd-resolve
  ├─540-systemd-timesyn
  ├─525-systemd-udevd
  ├─537-udisksd
  │   ├─538-{udisksd}
  │   ├─539-{udisksd}
  │   └─540-{udisksd}
  ├─547-unattended-upgr
  │   ├─548-{unattended-upgr}
  │   └─549-{unattended-upgr}
  ├─535-upowerd
  │   ├─536-{upowerd}
  │   └─537-{upowerd}
  ├─586-user-manager
  │   ├─587-{user-manager}
  │   ├─588-{user-manager}
  │   └─589-{user-manager}
  └─550-whoopsie
      ├─551-{whoopsie}
      └─552-{whoopsie}

This groups the processes by their process group ID, which can be useful for understanding the relationships between processes.

You can also use the -H option to highlight the current process in the output:

$ pstree -H $$
systemd─┬─ModemManager─┬─dhclient
        │             └─2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─apache2─┬─apache2
        │         └─2*[apache2]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
        │         │            │                └─3*[{containerd-shim}]
        │         │            └─6*[{containerd}]
        │         └─7*[{dockerd}]
        ├─gdm3─┬─Xorg
        │      └─gnome-session-b─┬─gnome-shell
        │                        ├─ibus-daemon─┬─ibus-engine-sim
        │                        │             └─2*[{ibus-daemon}]
        │                        └─2*[{gnome-session-b}]
        ├─irqbalance
        ├─kerneloops
        ├─kube-proxy
        ├─kubelet
        ├─lightdm─┬─Xorg
        │         └─lightdm───sh───fluxbox
        ├─lxcfs
        ├─lxd─┬─lxd
        │     └─2*[{lxd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd
        ├─snapd───10*[{snapd}]
        ├─ssh-agent
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-networkd
        ├─systemd-resolve
        ├─systemd-timesyn
        ├─systemd-udevd
        ├─udisksd───3*[{udisksd}]
        ├─unattended-upgr───2*[{unattended-upgr}]
        ├─upowerd───2*[{upowerd}]
        ├─user-manager───3*[{user-manager}]
        └─whoopsie───2*[{whoopsie}]

This highlights the current process (the shell process in this case) in the output.

The pstree command provides a wealth of options for filtering and customizing the output to suit your specific needs. By exploring these options, you can gain a deeper understanding of the process hierarchy on your Linux system and more effectively monitor and manage your system's processes.

Summary

In this lab, we learned about the pstree command, which is a useful tool for visualizing the process hierarchy on a Linux system. We explored how to use pstree to understand the parent-child relationships between running processes, and how to filter and customize the output to focus on specific processes or process groups.

The lab covered the basics of the pstree command, including how to run it and interpret the tree-like output. We also learned how to use various options to filter the output, such as showing only specific processes or hiding certain process groups. By the end of the lab, we had a better understanding of how to use pstree to effectively analyze the process structure on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like