2. pstree 命令

pstree 命令以树状结构直观显示系统中的进程层级关系,清晰展示父子进程的依赖关系。

命令格式

pstree [选项] [用户名]

不给出 用户名 默认显示所有进程。

示例1

显示所有进程的进程树

weimz@mzstudio:~$ pstree
systemd─┬─ModemManager───3*[{ModemManager}]
        ├─NetworkManager───3*[{NetworkManager}]
        ├─VGAuthService
        ├─accounts-daemon───3*[{accounts-daemon}]
        ├─avahi-daemon───avahi-daemon
        ├─chronyd-starter───chronyd───chronyd
        ├─colord───3*[{colord}]
        ├─cron
        ├─cups-browsed───3*[{cups-browsed}]
        ├─cupsd
        ├─dbus-daemon
        ├─fwupd───3*[{fwupd}]
        ├─gdm3─┬─gdm-session-wor─┬─gdm-wayland-ses─┬─gnome-session-i───3*[{gnome-session-i}]
                                                └─3*[{gdm-wayland-ses}]
                               └─3*[{gdm-session-wor}]
              └─4*[{gdm3}]
...

常用选项

选项
说明
-p
显示 PID(进程ID)。
-u
显示用户名(标识进程所有者)。
-a
显示完整命令行(包括参数)。
-n
按 PID 数字排序(默认按进程名排序)。
-h
高亮当前进程及其祖先。

显示用户 weimz 创建的进程的进程树。

weimz@mzstudio:~$ pstree weimz
gdm-wayland-ses─┬─gnome-session-i───3*[{gnome-session-i}]
                └─3*[{gdm-wayland-ses}]

systemd─┬─(sd-pam)
        ├─at-spi-bus-laun─┬─dbus-daemon
                         └─4*[{at-spi-bus-laun}]
        ├─at-spi2-registr───3*[{at-spi2-registr}]
        ├─dbus-daemon
        ├─dconf-service───3*[{dconf-service}]
        ├─evolution-addre───6*[{evolution-addre}]
        ├─evolution-alarm───7*[{evolution-alarm}]
        ├─evolution-calen───9*[{evolution-calen}]
        ├─evolution-sourc───4*[{evolution-sourc}]
        ├─gcr-ssh-agent───2*[{gcr-ssh-agent}]
        ├─2*[gjs───11*[{gjs}]]
        ├─gnome-keyring-d───4*[{gnome-keyring-d}]
        ├─gnome-session-c───{gnome-session-c}
        ├─gnome-session-s───4*[{gnome-session-s}]
        ├─gnome-shell─┬─Xwayland
                     ├─gjs─┬─ptyxis─┬─ptyxis-agent─┬─bash───pstree
                                                └─4*[{ptyxis-agent}]
                                  └─12*[{ptyxis}]
                          └─16*[{gjs}]
                     ├─mutter-x11-fram───5*[{mutter-x11-fram}]
                     └─20*[{gnome-shell}]
        ├─gnome-shell-cal───6*[{gnome-shell-cal}]
...
        └─xdg-permission-───3*[{xdg-permission-}]

练习:

  1. 使用 pstree 显示进程树信息。
  2. 使用 pstree -p 显示进程树和 PID 信息。
  3. 使用 pstree -a 显示进程树、并显示进程启动的命令行信息。