In Linux, each running process is spawned by a parent process, except for the initial init process, which is started by the kernel at boot. This results in a hierarchical structure where processes are organized in a tree format, with parent processes at the top and their child processes branching out below them.
To view this tree-like structure, Linux provides tools like ps and pstree. These commands show how processes are grouped, with child processes listed under their corresponding parent processes. This is helpful for understanding the relationships between processes and for monitoring system performance.
The ps command lists running processes and can display them in a hierarchical view. The pstree command goes further by visually representing the process tree in a clearer and more organized format. Both tools are useful for managing processes and troubleshooting system issues.
Steps to display process tree in Linux:
- Open the terminal in Linux.
- Use the ps command to display running processes.
$ ps -x PID TTY STAT TIME COMMAND 1080 ? Ss 0:00 /lib/systemd/systemd --user 1081 ? S 0:00 (sd-pam) 1092 tty1 S+ 0:00 -bash 1175 ? S 0:00 sshd: user@pts/0 1176 pts/0 Ss 0:00 -bash 1424 pts/0 R+ 0:00 ps -x
- Use the –forest option with the ps command to display processes in a tree structure.
$ ps -x --forest PID TTY STAT TIME COMMAND 1175 ? S 0:00 sshd: user@pts/0 1176 pts/0 Ss 0:00 \_ -bash 1436 pts/0 R+ 0:00 \_ ps -x --forest 1092 tty1 S+ 0:00 -bash 1080 ? Ss 0:00 /lib/systemd/systemd --user 1081 ? S 0:00 \_ (sd-pam)
You can use the ps -aux –forest command to see more detailed information about all processes.
Manual: ps manual
- Install pstree if it is not available on your system.
$ sudo apt update && sudo apt install --assume-yes psmisc # Ubuntu and Debian $ sudo yum install --assumeyes pstree # CentOS and Red Hat
- Use the pstree command to view the process tree.
$ pstree systemd─┬─VGAuthService ├─accounts-daemon───2*[{accounts-daemon}] ├─atd ├─cron ├─dbus-daemon ├─login───bash ├─multipathd───6*[{multipathd}] ├─networkd-dispat ├─packagekitd───2*[{packagekitd}] ├─polkitd───2*[{polkitd}] ├─rsyslogd───3*[{rsyslogd}] ├─snapd───8*[{snapd}] ├─sshd───sshd───sshd───bash───pstree ├─systemd───(sd-pam) ├─systemd-journal ├─systemd-logind ├─systemd-network ├─systemd-resolve ├─systemd-timesyn───{systemd-timesyn} ├─systemd-udevd ├─unattended-upgr───{unattended-upgr} └─vmtoolsd───{vmtoolsd}
You can use the pstree -p option to show process IDs along with the tree structure.
More options for pstree:
Usage: pstree [-acglpsStuZ] [ -h | -H PID ] [ -n | -N type ] [ -A | -G | -U ] [ PID | USER ] pstree -V Display a tree of processes. -a, --arguments show command line arguments -A, --ascii use ASCII line drawing characters -c, --compact don't compact identical subtrees -h, --highlight-all highlight current process and its ancestors -H PID, --highlight-pid=PID highlight this process and its ancestors -g, --show-pgids show process group ids; implies -c -G, --vt100 use VT100 line drawing characters -l, --long don't truncate long lines -n, --numeric-sort sort output by PID -N type, --ns-sort=type sort by namespace type (cgroup, ipc, mnt, net, pid, user, uts) -p, --show-pids show PIDs; implies -c -s, --show-parents show parents of the selected process -S, --ns-changes show namespace transitions -t, --thread-names show full thread names -T, --hide-threads hide threads, show only processes -u, --uid-changes show uid transitions -U, --unicode use UTF-8 (Unicode) line drawing characters -V, --version display version information -Z, --security-context show SELinux security contexts PID start at this PID; default is 1 (init) USER show only trees rooted at processes of this user
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.