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:

  1. Open a terminal application, such as GNOME Terminal or Konsole.
  2. Use the ps command to list the running processes owned by you.
    $ ps -x
      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
  3. Use the ps command to list these processes in a hierarchical structure.
    $ ps -x --forest
      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

  4. 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
  5. Utilize the pstree command to display the processes in a tree-like format.
    $ pstree

    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
Discuss the article:

Comment anonymously. Login not required.