In Linux, every process originates from the init process, with the exception of init itself. The kernel initiates the init process during system boot, which then generates and manages other processes. These processes form a hierarchical parent-child relationship, resembling a tree-like structure.
You can display this process tree in Linux, with child processes grouped under their respective parent processes, by using the ps and pstree commands in the terminal.
Steps to show process tree in Linux:
- Open a terminal application, such as GNOME Terminal or Konsole.
- Use the ps command to list the running processes owned by you.
$ 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 ps command to list these processes in a hierarchical 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)
More options could be added to the command such as ps -aux --forest to see details of more processes
Manual: ps manual
- Install pstree if it's not already present on your system.
$ sudo apt update && sudo apt install --assume-yes psmisc # Ubuntu and Debian $ sudo yum install --assumeyes pstree # CentOS and Red Hat
- Utilize the pstree command to display the processes in a tree-like format.
$ 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}
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
Author: Mohd
Shakir Zakaria
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.
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.
Discuss the article:
Comment anonymously. Login not required.