How to show process tree in Linux
Every process in Linux is started by another process except for init. init is the first process in Linux where the kernel executes it during system boot. init then execute or create other processes, which in turn create other processes.
These processes run in a parent-child relationship or a tree-like structure. You can show a process tree where child processes are grouped by their parent process in Linux using ps and pstree command at the terminal.
Steps to show process tree in Linux:
-
Launch a terminal application such as GNOME Terminal or konsole.
-
List running processes owned by you using ps.
$ 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
-
List these processes using ps in a tree format.
$ 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 installed.
$ sudo apt update && sudo apt install --assume-yes psmisc #Ubuntu and Debian $ sudo yum install --assumeyes pstree #CentOS and Red Hat
-
List processes in a tree format using pstree.
$ 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