Another process starts every process in Linux except for init. init is the first process in Linux where the kernel executes it during system boot. init then runs or creates 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:

  1. Launch a terminal application such as GNOME Terminal or konsole.
  2. List running processes owned by you using ps.
    $ 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. List these processes using ps in a tree format.
    $ 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 installed.
    $ sudo apt update && sudo apt install --assume-yes psmisc #Ubuntu and Debian
    $ sudo yum install --assumeyes pstree #CentOS and Red Hat
  5. List processes in a tree format using pstree.
    $ 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.