Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

pstree

Show the running processes as a tree: who started whom.

Level
Everyday Linux
Group
Processes and jobs
Needs root
No
Package
psmisc

What it does

pstree draws the processes from systemd (PID 1) down. Identical children are compacted: httpd───5*[httpd] means five httpd children. Use -p for PIDs and -a for the full command lines.

How to write it

pstree [OPTION]... [PID | USER]

Options 20

Most used

-p
Show PIDs (turns off compaction).
-a
Show the command line arguments.
-u
Show user changes, like (apache).
-s
Show the parents of the given PID.
-h
Highlight the current process and its parents.
-H PID
Highlight this process.
-n
Sort by PID.
-c
Do not compact identical subtrees.
-l
Long lines (no cut).
-g
Show process group IDs.
-t
Full names of threads.
-T
Hide threads.
-A
ASCII lines.
-G
VT100 lines.
-U
UTF-8 lines.
-C age
Color by age.
-N TYPE
One tree per namespace (ipc, mnt, net, pid, user, uts, cgroup, time).
-S
Show namespace changes.
-Z
Show the SELinux context.
-V
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1The whole tree

    pstree | head
    systemd─┬─NetworkManager───3*[{NetworkManager}]
            ├─chronyd
            ├─sshd───sshd───sshd───bash───pstree

    {…} are threads.

  2. 2With PIDs

    pstree -p | head

    Find the PID of a parent quickly.

  3. 3Tree of one process

    pstree -p 1010

    Only the sshd master and its sessions.

  4. 4Where does my shell come from?

    pstree -s -p $$
    systemd(1)───sshd(1010)───sshd(2210)───sshd(2215)───bash(2216)

    The chain of parents.

  5. 5Show the user of each part

    pstree -u

    (apache) shows where a process changed to another user.

  6. 6Full command lines

    pstree -a | less

    Shows the options of each program.

  7. 7Processes of one user

    pstree lisa

    All trees owned by lisa.

  8. 8Find odd children of a web server (security)

    pstree -p -a $(pgrep -o httpd)

    A shell (bash, sh) under httpd can mean a web shell attack.

  9. 9Do not compact

    pstree -c -p | grep httpd

    Every child on its own line.

  10. 10Container namespaces

    pstree -N pid

    Separate trees for each PID namespace (containers).

Exit codes

0Done.
1Error.