Using the pstree Command (with examples)

Using the pstree Command (with examples)

The pstree command is a useful tool for displaying a tree-like representation of running processes on a Unix-like system. It provides a visual overview of the process hierarchy and relationships, making it easier to understand the structure of the system’s processes.

Displaying a tree of processes

To display a tree of processes, you can simply run the pstree command without any arguments:

pstree

Motivation: This use case is helpful when you want to get a quick overview of the running processes on your system. It provides a clear visualization of how processes are related to each other and can help identify any potential issues or inefficiencies.

Explanation: Running pstree without any arguments will display a tree-like structure of processes, with each process represented as a node and its child processes indented below it. The tree starts with the init process (PID 1) at the top and branches out into different processes.

Example output:

systemd─┬─ModemManager───{ModemManager}
        ├─NetworkManager─┬─dhclient
        │                └─{NetworkManager}
        ├─accounts-daemon───{accounts-daemon}
        ├─avahi-daemon───avahi-daemon
        ├─colord───2*[{colord}]
        ├─cron
        ├─dbus-daemon
        ├─2*[gdm-session-wor]
        ├─gnome-keyring-d───{gnome-keyring-d}
        ├─gpg-agent
        ├─4*[{gpg-agent}]
        ├─gvfs-afc-volume───{gvfs-afc-volume}
        ├─gvfs-goa-volume───{gvfs-goa-volume}
        ├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
        ├─gvfs-mtp-volume───{gvfs-mtp-volume}
        ├─gvfs-udisks2-vo───{gvfs-udisks2-vo}
        ├─gvfsd───{gvfsd}
        ├─gvfsd-burn───{gvfsd-burn}
        ├─gvfsd-dnssd───{gvfsd-dnssd}
        ├─gvfsd-fuse───4*[{gvfsd-fuse}]
        ├─gvfsd-metadata───{gvfsd-metadata}
        ├─gvfsd-recent───{gvfsd-recent}
        ├─gvfsd-trash───{gvfsd-trash}
        ├─ibus-dconf───────2*[{ibus-dconf}]
        ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        ├─ibus-extension───{ibus-extension}
        ├─ibus-ui-gtk3───5*[{ibus-ui-gtk3}]
        ├─3*[ibus-x11───{ibus-x11}]
        ├─irqbalance───{irqbalance}
        ├─lightdm─┬─Xorg───2*[{Xorg}]
        │         ├─lightdm─┬─gnome-session─┬─gnome-settings─┬─gsd-a11y-keyboa─┬─{gsd-a11y-keyboa}
        │         │         │                ├─gsd-color───2*[{gsd-color}]
        │         │         │                ├─gsd-datetime───2*[{gsd-datetime}]
        │         │         │                ├─gsd-housekeepi───2*[{gsd-housekeepi}]
        │         │         │                ├─gsd-keyboard───2*[{gsd-keyboard}]
        │         │         │                ├─gsd-media-keys───2*[{gsd-media-keys}]
        │         │         │                ├─gsd-mouse───2*[{gsd-mouse}]
        │         │         │                ├─gsd-power───2*[{gsd-power}]
        │         │         │                ├─gsd-print-notif───2*[{gsd-print-notif}]
        │         │         │                ├─gsd-rfkill───2*[{gsd-rfkill}]
        │         │         │                ├─gsd-screensaver───2*[{gsd-screensaver}]
        │         │         │                ├─gsd-sharing───2*[{gsd-sharing}]
        │         │         │                ├─gsd-smartcard───2*[{gsd-smartcard}]
        │         │         │                ├─gsd-sound───2*[{gsd-sound}]
        │         │         │                ├─gsd-wacom─┬─{gsd-wacom}
        │         │         │                │           └─{wacom-input-dev}
        │         │         │                ├─gsd-wacom-pad───2*[{gsd-wacom-pad}]
        │         │         │                ├─gsd-xsettings───2*[{gsd-xsettings}]
        │         │         │                ├─nautilus-deskto───2*[{nautilus-deskto}]
        │         │         │                ├─polkit-gnome-au───{polkit-gnome-au}
        │         │         │                ├─tracker-miner-f───{tracker-miner-f}
        │         │         │                └─3*[{gnome-session-}]
        │         │         └─3*[{lightdm}]
        │         └─{lightdm}
        ├─polkitd───{polkitd}
        ├─pulseaudio───4*[{pulseaudio}]
        ├─2*[rtkit-daemon───{rtkit-daemon}]
        ├─runit───runit─┬─dbus-daemon
        │                ├─rabbitmq-server─┬─beam.smp─┬─inet_gethost
        │                │                │          └─11*[{beam.smp}]
        │                │                └─2*[{rabbitmq-server}]
        │                ├─redis-server───4*[{redis-server}]
        │                ├─syslog-ng───3*[{syslog-ng}]
        │                └─2*[{runit}]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─systemd-userdbd
        ├─systemd─┬─(sd-pam)
        │         ├─dunst
        │         └─{systemd}
        ├─udisksd───4*[{udisksd}]
        ├─udisksd-indica───3*[{udisksd-indica}]
        ├─upowerd───{upowerd}
        ├─whoopsie───{whoopsie}
        └─wpa_supplicant

Displaying a tree of processes with PIDs

To display a tree of processes along with their PIDs, you can use the -p option:

pstree -p

Motivation: This use case is particularly useful when you need to identify specific processes by their PIDs. It allows you to easily correlate the process tree structure with their corresponding process IDs.

Explanation: Adding the -p option to the pstree command will display each process with its corresponding PID in brackets. This additional information can be helpful when troubleshooting or performing system analysis.

Example output:

systemd(1)─┬─ModemManager(681)───{ModemManager}
           ├─NetworkManager(572)─┬─dhclient(1119)
           │                     └─{NetworkManager}
           ├─accounts-daemon(711)───{accounts-daemon}
           ├─avahi-daemon(622)───avahi-daemon(6554)
           ├─colord(585)───2*[{colord}]
           ├─cron(856)
           ├─dbus-daemon(569)
           ├─2*[gdm-session-wor(689)]
           ├─gnome-keyring-d(703)───{gnome-keyring-d}
           ├─gpg-agent(723)
           ├─4*[{gpg-agent}]
           ├─gvfs-afc-volume(804)───{gvfs-afc-volume}
           ├─gvfs-goa-volume(803)───{gvfs-goa-volume}
           ├─gvfs-gphoto2-vo(805)───{gvfs-gphoto2-vo}
           ├─gvfs-mtp-volume(806)───{gvfs-mtp-volume}
           ├─gvfs-udisks2-vo(807)───{gvfs-udisks2-vo}
           ├─gvfsd(709)───{gvfsd}
           ├─gvfsd-burn(746)───{gvfsd-burn}
           ├─gvfsd-dnssd(724)───{gvfsd-dnssd}
           ├─gvfsd-fuse(711)───4*[{gvfsd-fuse}]
           ├─gvfsd-metadata(789)───{gvfsd-metadata}
           ├─gvfsd-recent(710)───{gvfsd-recent}
           ├─gvfsd-trash(727)───{gvfsd-trash}
           ├─ibus-dconf(831)───────2*[{ibus-dconf}]
           ├─ibus-engine-sim(828)───2*[{ibus-engine-sim}]
           ├─ibus-extension(829)───{ibus-extension}
           ├─ibus-ui-gtk3(710)───5*[{ibus-ui-gtk3}]
           ├─3*[ibus-x11(830)───{ibus-x11}]
           ├─irqbalance(834)───{irqbalance}
           ├─lightdm(681)─┬─Xorg(805)───2*[{Xorg}]
           │              ├─lightdm(717)─┬─gnome-session(798)─┬─gnome-settings-(910)─┬─gsd-a11y-keyboar─┬─{gsd-a11y-keyboar}
           │              │               │                     ├─gsd-color(913)───2*[{gsd-color}]
           │              │               │                     ├─gsd-datetime(918)───2*[{gsd-datetime}]
           │              │               │                     ├─gsd-housekeepin(925)───2*[{gsd-housekeepin}]
           │              │               │                     ├─gsd-keyboard(926)───2*[{gsd-keyboard}]
           │              │               │                     ├─gsd-media-keys(927)───2*[{gsd-media-keys}]
           │              │               │                     ├─gsd-mouse(928)───2*[{gsd-mouse}]
           │              │               │                     ├─gsd-power(929)───2*[{gsd-power}]
           │              │               │                     ├─gsd-print-notif(930)───2*[{gsd-print-notif}]
           │              │               │                     ├─gsd-rfkill(931)───2*[{gsd-rfkill}]
           │              │               │                     ├─gsd-screensaver(932)───2*[{gsd-screensaver}]
           │              │               │                     ├─gsd-sharing(933)───2*[{gsd-sharing}]
           │              │               │                     ├─gsd-smartcard(935)───2*[{gsd-smartcard}]
           │              │               │                     ├─gsd-sound(937)───2*[{gsd-sound}]
           │              │               │                     ├─gsd-wacom(938)─┬─{gsd-wacom}
           │              │               │                     │                └─{wacom-input-dev}
           │              │               │                     ├─gsd-wacom-pad(936)───2*[{gsd-wacom-pad}]
           │              │               │                     ├─gsd-xsettings(983)───2*[{gsd-xsettings}]
           │              │               │                     ├─nautilus-deskto(3049)───2*[{nautilus-deskto}]
           │              │               │                     ├─polkit-gnome-au(998)───{polkit-gnome-au}
           │              │               │                     ├─tracker-miner-f(1005)───{tracker-miner-f}
           │              │               │                     └─3*[{gnome-session-}]
           │              │               └─3*[{lightdm}]
           │              └─{lightdm}
           ├─polkitd(747)───{polkitd}
           ├─pulseaudio(716)───4*[{pulseaudio}]
           ├─2*[rtkit-daemon(746)───{rtkit-daemon}]
           ├─runit(717)───runit(656)─┬─dbus-daemon(742)
           │                        ├─rabbitmq-server(56)─┬─beam.smp(59)─┬─inet_gethost(2345)
           │                        │                   │               └─11*[{beam.smp}]
           │                        │                   └─2*[{rabbitmq-server}]
           │                        ├─redis-server(757)───4*[{redis-server}]
           │                        ├─syslog-ng(741)───3*[{syslog-ng}]
           │                        └─2*[{runit(656)}]
           ├─systemd-journal(492)
           ├─systemd-logind(564)
           ├─systemd-udevd(373)
           ├─systemd-userdbd(479)
           ├─systemd(489)─┬─(sd-pam)(493)
           │              ├─dunst(799)
           │              └─{systemd}(490)
           ├─udisksd(623)───4*[{udisksd}]
           ├─udisksd-indica(806)───3*[{udisksd-indica}]
           ├─upowerd(749)───{upowerd}
           ├─whoopsie(893)───{whoopsie}
           └─wpa_supplicant(578)

Displaying all process trees rooted at processes owned by a specified user

To display all process trees rooted at processes owned by a specific user, you can provide the user’s username as an argument to pstree:

pstree user

Motivation: This use case is beneficial when you want to narrow down the process tree analysis to a specific user’s processes. It helps to visualize the processes and their relationships within that user’s context.

Explanation: By specifying a username as an argument to pstree, the command will display the process trees rooted at the processes owned by that user. This allows you to focus on a particular user’s processes and understand their relationships within the overall system.

Example output (assuming the specified user is “john”):

systemd─┬─ModemManager───{ModemManager}
        ├─NetworkManager─┬─dhclient
        │                └─{NetworkManager}
        ├─accounts-daemon───{accounts-daemon}
        ├─colord───2*[{colord}]
        ├─cron
        ├─dbus-daemon
        ├─2*[gdm-session-wor]
        ├─gnome-keyring-d───{gnome-keyring-d}
        ├─gpg-agent
        ├─4*[{gpg-agent}]
        ├─gvfs-afc-volume───{gvfs-afc-volume}
        ├─gvfs-goa-volume───{gvfs-goa-volume}
        ├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
        ├─gvfs-mtp-volume───{gvfs-mtp-volume}
        ├─gvfs-udisks2-vo───{gvfs-udisks2-vo}
        ├─gvfsd───{gvfsd}
        ├─gvfsd-burn───{gvfsd-burn}
        ├─gvfsd-dnssd───{gvfsd-dnssd}
        ├─gvfsd-fuse───4*[{gvfsd-fuse}]
        ├─gvfsd-metadata───{gvfsd-metadata}
        ├─gvfsd-recent───{gvfsd-recent}
        ├─gvfsd-trash───{gvfsd-trash}
        ├─ibus-dconf───────2*[{ibus-dconf}]
        ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        ├─ibus-extension───{ibus-extension}
        ├─ibus-ui-gtk3───5*[{ibus-ui-gtk3}]
        ├─3*[ibus-x11───{ibus-x11}]
        ├─irqbalance───{irqbalance}
        ├─lightdm─┬─Xorg
        │         ├─lightdm─┬─gnome-session─┬─gnome-settings─┬─gsd-a11y-keyboa─┬─{gsd-a11y-keyboa}
        │         │         │                ├─gsd-color───2*[{gs

Related Posts

How to Use the Dolt Blame Command (with examples)

How to Use the Dolt Blame Command (with examples)

The Dolt Blame command is a powerful tool that allows users to view commit information for each row of a Dolt table.

Read More
Managing Private Registries with Azure Container Registries (with examples)

Managing Private Registries with Azure Container Registries (with examples)

Introduction Azure Container Registry (ACR) is a managed private registry service provided by Microsoft Azure.

Read More
Dockerd Command (with examples)

Dockerd Command (with examples)

Introduction The dockerd command is used to start and manage Docker containers.

Read More