介绍
在本实验中,我们将探索 Linux 的 pstree 命令,这是一个用于可视化 Linux 系统上进程层次结构的有用工具。pstree 命令以树状形式显示正在运行的进程,展示它们之间的父子关系。这对于理解系统进程的结构和依赖关系非常有帮助。我们将从理解 pstree 命令开始,然后使用 pstree 探索进程层次结构,最后学习如何过滤和自定义 pstree 的输出。
在本实验中,我们将探索 Linux 的 pstree 命令,这是一个用于可视化 Linux 系统上进程层次结构的有用工具。pstree 命令以树状形式显示正在运行的进程,展示它们之间的父子关系。这对于理解系统进程的结构和依赖关系非常有帮助。我们将从理解 pstree 命令开始,然后使用 pstree 探索进程层次结构,最后学习如何过滤和自定义 pstree 的输出。
在这一步中,我们将学习 pstree 命令,这是一个用于可视化 Linux 系统上进程层次结构的有用工具。
pstree 命令以树状形式显示正在运行的进程,展示它们之间的父子关系。这对于理解系统进程的结构和依赖关系非常有帮助。
首先,让我们在终端中运行 pstree 命令:
$ pstree
systemd─┬─ModemManager─┬─dhclient
│ └─2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─apache2─┬─apache2
│ └─2*[apache2]
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ │ └─3*[{containerd-shim}]
│ │ └─6*[{containerd}]
│ └─7*[{dockerd}]
├─gdm3─┬─Xorg
│ └─gnome-session-b─┬─gnome-shell
│ ├─ibus-daemon─┬─ibus-engine-sim
│ │ └─2*[{ibus-daemon}]
│ └─2*[{gnome-session-b}]
├─irqbalance
├─kerneloops
├─kube-proxy
├─kubelet
├─lightdm─┬─Xorg
│ └─lightdm───sh───fluxbox
├─lxcfs
├─lxd─┬─lxd
│ └─2*[{lxd}]
├─polkitd───2*[{polkitd}]
├─rsyslogd
├─snapd───10*[{snapd}]
├─ssh-agent
├─systemd-journal
├─systemd-logind
├─systemd-networkd
├─systemd-resolve
├─systemd-timesyn
├─systemd-udevd
├─udisksd───3*[{udisksd}]
├─unattended-upgr───2*[{unattended-upgr}]
├─upowerd───2*[{upowerd}]
├─user-manager───3*[{user-manager}]
└─whoopsie───2*[{whoopsie}]
输出显示了正在运行的进程的层次结构视图,其中 systemd 进程位于顶部,作为许多其他进程的父进程。
你可以使用各种选项自定义 pstree 的输出。例如,要仅显示进程 ID(PID)而不是进程名称,可以使用 -p 选项:
$ pstree -p
systemd(1)─┬─ModemManager(577)─┬─dhclient(593)
│ └─2*[{ModemManager}(580,581)]
├─NetworkManager(572)─┬─dhclient(590)
│ └─2*[{NetworkManager}(573,574)]
├─accounts-daemon(531)───2*[{accounts-daemon}(532,533)]
├─apache2(688)─┬─apache2(689)
│ └─2*[apache2(690,691)]
├─atd(544)
├─cron(545)
├─dbus-daemon(530)
├─dockerd(688)─┬─containerd(701)─┬─containerd-shim(702)─┬─bash(703)
│ │ │ └─3*[{containerd-shim}(704,705,706)]
│ │ └─6*[{containerd}(707,708,709,710,711,712)]
│ └─7*[{dockerd}(689,690,691,692,693,694,695)]
├─gdm3(575)─┬─Xorg(586)
│ └─gnome-session-b(587)─┬─gnome-shell(588)
│ ├─ibus-daemon(589)─┬─ibus-engine-sim(592)
│ │ └─2*[{ibus-daemon}(590,591)]
│ └─2*[{gnome-session-b}(595,596)]
├─irqbalance(543)
├─kerneloops(546)
├─kube-proxy(697)
├─kubelet(696)
├─lightdm(576)─┬─Xorg(585)
│ └─lightdm(577)───sh(578)───fluxbox(579)
├─lxcfs(542)
├─lxd(698)─┬─lxd(699)
│ └─2*[{lxd}(700,701)]
├─polkitd(534)───2*[{polkitd}(535,536)]
├─rsyslogd(541)
├─snapd(694)───10*[{snapd}(695,696,697,698,699,700,701,702,703,704)]
├─ssh-agent(587)
├─systemd-journal(526)
├─systemd-logind(529)
├─systemd-networkd(527)
├─systemd-resolve(528)
├─systemd-timesyn(540)
├─systemd-udevd(525)
├─udisksd(537)───3*[{udisksd}(538,539,540)]
├─unattended-upgr(547)───2*[{unattended-upgr}(548,549)]
├─upowerd(535)───2*[{upowerd}(536,537)]
├─user-manager(586)───3*[{user-manager}(587,588,589)]
└─whoopsie(550)───2*[{whoopsie}(551,552)]
这将显示进程 ID 而不是进程名称。
你还可以使用 -u 选项来显示运行每个进程的用户:
$ pstree -u
systemd─┬─ModemManager─┬─dhclient
│ └─2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─apache2─┬─apache2
│ └─2*[apache2]
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ │ └─3*[{containerd-shim}]
│ │ └─6*[{containerd}]
│ └─7*[{dockerd}]
├─gdm3─┬─Xorg
│ └─gnome-session-b─┬─gnome-shell
│ ├─ibus-daemon─┬─ibus-engine-sim
│ │ └─2*[{ibus-daemon}]
│ └─2*[{gnome-session-b}]
├─irqbalance
├─kerneloops
├─kube-proxy
├─kubelet
├─lightdm─┬─Xorg
│ └─lightdm───sh───fluxbox
├─lxcfs
├─lxd─┬─lxd
│ └─2*[{lxd}]
├─polkitd───2*[{polkitd}]
├─rsyslogd
├─snapd───10*[{snapd}]
├─ssh-agent
├─systemd-journal
├─systemd-logind
├─systemd-networkd
├─systemd-resolve
├─systemd-timesyn
├─systemd-udevd
├─udisksd───3*[{udisksd}]
├─unattended-upgr───2*[{unattended-upgr}]
├─upowerd───2*[{upowerd}]
├─user-manager───3*[{user-manager}]
└─whoopsie───2*[{whoopsie}]
这将显示运行每个进程的用户。
pstree 命令可以成为理解 Linux 系统上进程层次结构的宝贵工具。在下一步中,我们将探索更多使用 pstree 的方法,以深入了解系统的进程。
在这一步中,我们将深入使用 pstree 命令探索进程层次结构,并学习如何通过各种选项自定义输出。
首先,让我们看看如何使用 -c 选项以更紧凑的方式显示进程层次结构:
$ pstree -c
systemd─┬─ModemManager─┬─dhclient
│ └─2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─apache2─┬─apache2
│ └─2*[apache2]
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ │ └─3*[{containerd-shim}]
│ │ └─6*[{containerd}]
│ └─7*[{dockerd}]
├─gdm3─┬─Xorg
│ └─gnome-session-b─┬─gnome-shell
│ ├─ibus-daemon─┬─ibus-engine-sim
│ │ └─2*[{ibus-daemon}]
│ └─2*[{gnome-session-b}]
├─irqbalance
├─kerneloops
├─kube-proxy
├─kubelet
├─lightdm─┬─Xorg
│ └─lightdm───sh───fluxbox
├─lxcfs
├─lxd─┬─lxd
│ └─2*[{lxd}]
├─polkitd───2*[{polkitd}]
├─rsyslogd
├─snapd───10*[{snapd}]
├─ssh-agent
├─systemd-journal
├─systemd-logind
├─systemd-networkd
├─systemd-resolve
├─systemd-timesyn
├─systemd-udevd
├─udisksd───3*[{udisksd}]
├─unattended-upgr───2*[{unattended-upgr}]
├─upowerd───2*[{upowerd}]
├─user-manager───3*[{user-manager}]
└─whoopsie───2*[{whoopsie}]
-c 选项可以压缩输出,使其更易于阅读和理解进程层次结构。
接下来,让我们看看如何过滤输出以专注于特定进程。例如,要仅显示与 docker 服务相关的进程,可以使用 -a 选项:
$ pstree -a | grep docker
dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ └─3*[containerd-shim]
│ └─6*[containerd]
└─7*[dockerd]
这将显示 docker 进程及其子进程。
你还可以使用 -p 选项来显示进程 ID(PID)而不是进程名称:
$ pstree -p | grep docker
dockerd(688)─┬─containerd(701)─┬─containerd-shim(702)─┬─bash(703)
│ │ └─3*[containerd-shim(704,705,706)]
│ └─6*[containerd(707,708,709,710,711,712)]
└─7*[dockerd(689,690,691,692,693,694,695)]
当你需要通过 PID 识别特定进程时,这非常有用。
最后,让我们看看如何显示特定用户的进程层次结构。为此,我们可以使用 -u 选项后跟用户名:
$ pstree -u labex
systemd─┬─ModemManager─┬─dhclient
│ └─2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─apache2─┬─apache2
│ └─2*[apache2]
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ │ └─3*[{containerd-shim}]
│ │ └─6*[{containerd}]
│ └─7*[{dockerd}]
├─gdm3─┬─Xorg
│ └─gnome-session-b─┬─gnome-shell
│ ├─ibus-daemon─┬─ibus-engine-sim
│ │ └─2*[{ibus-daemon}]
│ └─2*[{gnome-session-b}]
├─irqbalance
├─kerneloops
├─kube-proxy
├─kubelet
├─lightdm─┬─Xorg
│ └─lightdm───sh───fluxbox
├─lxcfs
├─lxd─┬─lxd
│ └─2*[{lxd}]
├─polkitd───2*[{polkitd}]
├─rsyslogd
├─snapd───10*[{snapd}]
├─ssh-agent
├─systemd-journal
├─systemd-logind
├─systemd-networkd
├─systemd-resolve
├─systemd-timesyn
├─systemd-udevd
├─udisksd───3*[{udisksd}]
├─unattended-upgr───2*[{unattended-upgr}]
├─upowerd───2*[{upowerd}]
├─user-manager───3*[{user-manager}]
└─whoopsie───2*[{whoopsie}]
这将显示 labex 用户的进程层次结构。
pstree 命令提供了关于 Linux 系统上进程层次结构的丰富信息。通过使用各种选项,你可以根据需要自定义输出,并更深入地了解系统的进程。
在这最后一步中,我们将探索更多方法来过滤和自定义 pstree 命令的输出,以满足你的特定需求。
一个常见的用例是专注于某个特定进程或进程组。例如,假设你想查看 systemd 进程及其子进程的层次结构。你可以使用 -s 选项来实现:
$ pstree -s systemd
systemd
这将显示 systemd 进程及其直接子进程。
你还可以使用 -g 选项按进程组 ID(PGID)而不是进程层次结构来分组进程:
$ pstree -g
1-systemd
├─577-ModemManager
│ ├─593-dhclient
│ └─2*[580-{ModemManager},581-{ModemManager}]
├─572-NetworkManager
│ ├─590-dhclient
│ └─2*[573-{NetworkManager},574-{NetworkManager}]
├─531-accounts-daemon
│ ├─532-{accounts-daemon}
│ └─533-{accounts-daemon}
├─688-apache2
│ ├─689-apache2
│ └─2*[690-apache2,691-apache2]
├─544-atd
├─545-cron
├─530-dbus-daemon
├─688-dockerd
│ ├─701-containerd
│ │ ├─702-containerd-shim
│ │ │ └─703-bash
│ │ └─6*[707-{containerd},708-{containerd},709-{containerd},710-{containerd},711-{containerd},712-{containerd}]
│ └─7*[689-{dockerd},690-{dockerd},691-{dockerd},692-{dockerd},693-{dockerd},694-{dockerd},695-{dockerd}]
├─575-gdm3
│ ├─586-Xorg
│ └─587-gnome-session-b
│ ├─588-gnome-shell
│ ├─589-ibus-daemon
│ │ ├─592-ibus-engine-sim
│ │ └─2*[590-{ibus-daemon},591-{ibus-daemon}]
│ └─2*[595-{gnome-session-b},596-{gnome-session-b}]
├─543-irqbalance
├─546-kerneloops
├─697-kube-proxy
├─696-kubelet
├─576-lightdm
│ ├─585-Xorg
│ └─577-lightdm
│ ├─578-sh
│ └─579-fluxbox
├─542-lxcfs
├─698-lxd
│ ├─699-lxd
│ └─2*[700-{lxd},701-{lxd}]
├─534-polkitd
│ ├─535-{polkitd}
│ └─536-{polkitd}
├─541-rsyslogd
├─694-snapd
│ └─10*[695-{snapd},696-{snapd},697-{snapd},698-{snapd},699-{snapd},700-{snapd},701-{snapd},702-{snapd},703-{snapd},704-{snapd}]
├─587-ssh-agent
├─526-systemd-journal
├─529-systemd-logind
├─527-systemd-networkd
├─528-systemd-resolve
├─540-systemd-timesyn
├─525-systemd-udevd
├─537-udisksd
│ ├─538-{udisksd}
│ ├─539-{udisksd}
│ └─540-{udisksd}
├─547-unattended-upgr
│ ├─548-{unattended-upgr}
│ └─549-{unattended-upgr}
├─535-upowerd
│ ├─536-{upowerd}
│ └─537-{upowerd}
├─586-user-manager
│ ├─587-{user-manager}
│ ├─588-{user-manager}
│ └─589-{user-manager}
└─550-whoopsie
├─551-{whoopsie}
└─552-{whoopsie}
这将按进程组 ID 对进程进行分组,这对于理解进程之间的关系非常有用。
你还可以使用 -H 选项在输出中高亮显示当前进程:
$ pstree -H $$
systemd─┬─ModemManager─┬─dhclient
│ └─2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─apache2─┬─apache2
│ └─2*[apache2]
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─bash
│ │ │ └─3*[{containerd-shim}]
│ │ └─6*[{containerd}]
│ └─7*[{dockerd}]
├─gdm3─┬─Xorg
│ └─gnome-session-b─┬─gnome-shell
│ ├─ibus-daemon─┬─ibus-engine-sim
│ │ └─2*[{ibus-daemon}]
│ └─2*[{gnome-session-b}]
├─irqbalance
├─kerneloops
├─kube-proxy
├─kubelet
├─lightdm─┬─Xorg
│ └─lightdm───sh───fluxbox
├─lxcfs
├─lxd─┬─lxd
│ └─2*[{lxd}]
├─polkitd───2*[{polkitd}]
├─rsyslogd
├─snapd───10*[{snapd}]
├─ssh-agent
├─systemd-journal
├─systemd-logind
├─systemd-networkd
├─systemd-resolve
├─systemd-timesyn
├─systemd-udevd
├─udisksd───3*[{udisksd}]
├─unattended-upgr───2*[{unattended-upgr}]
├─upowerd───2*[{upowerd}]
├─user-manager───3*[{user-manager}]
└─whoopsie───2*[{whoopsie}]
这将在输出中高亮显示当前进程(在本例中为 shell 进程)。
pstree 命令提供了丰富的选项来过滤和自定义输出,以满足你的特定需求。通过探索这些选项,你可以更深入地了解 Linux 系统上的进程层次结构,并更有效地监控和管理系统进程。
在本实验中,我们学习了 pstree 命令,这是一个用于可视化 Linux 系统上进程层次结构的有用工具。我们探索了如何使用 pstree 来理解运行中进程之间的父子关系,以及如何过滤和自定义输出以专注于特定进程或进程组。
实验涵盖了 pstree 命令的基础知识,包括如何运行它并解释树状输出。我们还学习了如何使用各种选项来过滤输出,例如仅显示特定进程或隐藏某些进程组。通过本实验,我们更好地理解了如何使用 pstree 来有效分析 Linux 系统上的进程结构。