Linux pstree命令將所有行程以樹狀圖顯示,樹狀圖將會以 pid (如果有指定) 或是以 init 這個基本行程為根 (root),如果有指定使用者 id,則樹狀圖會只顯示該使用者所擁有的行程。
用法:
[hejianping@centos ~]$ pstree --help pstree: unrecognized option '--help' Usage: pstree [ -a ] [ -c ] [ -h | -H PID ] [ -l ] [ -n ] [ -p ] [ -g ] [ -u ] [ -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 (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 -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
常用參數說明:
-a 顯示每個程序的完整指令,包含路徑,參數或是常駐服務的標示。 -c 不使用精簡標示法。 -G 使用VT100終端機的列繪圖字符。 -h 列出樹狀圖時,特別標明現在執行的程序。 -H <程序識別碼> 此參數的效果和指定”-h”參數類似,但特別標明指定的程序。 -l 采用長列格式顯示樹狀圖。 -n 用程序識別碼排序。預設是以程序名稱來排序。 -p 顯示程序識別碼。 -u 顯示用戶名稱。 -U 使用UTF-8列繪圖字符。 -V 顯示版本信息。
直接輸入pstree顯示進程的關系:
hejianping@VM-0-2-ubuntu:~$ pstree systemd─┬─YDLive───{YDLive} ├─YDService───9*[{YDService}] ├─accounts-daemon─┬─{gdbus} │ └─{gmain} ├─acpid ├─2*[agetty] ├─atd ├─barad_agent─┬─barad_agent │ └─barad_agent───3*[{barad_agent}] ├─cron ├─dbus-daemon ├─dhclient ├─2*[iscsid] ├─lvmetad ├─lxcfs───6*[{lxcfs}] ├─mdadm ├─nginx───nginx ├─polkitd─┬─{gdbus} │ └─{gmain} ├─rsyslogd─┬─{in:imklog} │ ├─{in:imuxsock} │ └─{rs:main Q:Reg} ├─sgagent───{sgagent} ├─snapd───6*[{snapd}] ├─sshd───sshd───sshd─┬─bash───pstree │ └─bash ├─supervisord───ssserver ├─2*[systemd───(sd-pam)] ├─systemd-journal ├─systemd-logind ├─systemd-timesyn───{sd-resolve} └─systemd-udevd
pstree -p 顯示對應的進程pid:
hejianping@VM-0-2-ubuntu:~$ pstree -p systemd(1)─┬─YDLive(13705)───{YDLive}(13714) ├─YDService(8725)─┬─{YDService}(8726) │ ├─{YDService}(8727) │ ├─{YDService}(8728) │ ├─{YDService}(8865) │ ├─{YDService}(8866) │ ├─{YDService}(8874) │ ├─{YDService}(8880) │ ├─{YDService}(8882) │ └─{YDService}(8883) ├─accounts-daemon(867)─┬─{gdbus}(1018) │ └─{gmain}(1011) ├─acpid(855) ├─agetty(1163) ├─agetty(1164) ├─atd(838) ├─barad_agent(5980)─┬─barad_agent(5982) │ └─barad_agent(5983)─┬─{barad_agent}(6008) │ ├─{barad_agent}(6010) │ └─{barad_agent}(25197) ├─cron(875) ├─dbus-daemon(761) ├─dhclient(803) ├─iscsid(1016) ├─iscsid(1017) ├─lvmetad(375) ├─lxcfs(871)─┬─{lxcfs}(916) │ ├─{lxcfs}(917) │ ├─{lxcfs}(2401) │ ├─{lxcfs}(2402) │ ├─{lxcfs}(2404) │ └─{lxcfs}(2429) ├─mdadm(946) ├─nginx(3912)───nginx(19881) ├─polkitd(1035)─┬─{gdbus}(1057) │ └─{gmain}(1055) ├─rsyslogd(864)─┬─{in:imklog}(1014) │ ├─{in:imuxsock}(1013) │ └─{rs:main Q:Reg}(1015) ├─sgagent(1425)───{sgagent}(1426) ├─snapd(861)─┬─{snapd}(953) │ ├─{snapd}(960) │ ├─{snapd}(1020) │ ├─{snapd}(1021) │ ├─{snapd}(1022) │ └─{snapd}(16061) ├─sshd(984)───sshd(9116)───sshd(9180)─┬─bash(9182)───pstree(25198) │ └─bash(15828) ├─supervisord(15736)───ssserver(15760) ├─systemd(3787)───(sd-pam)(3792) ├─systemd(9123)───(sd-pam)(9128) ├─systemd-journal(337) ├─systemd-logind(843) ├─systemd-timesyn(608)───{sd-resolve}(619) └─systemd-udevd(418)
pstree -p <pid> 查看某個進程的樹型結構:
hejianping@VM-0-2-ubuntu:~$ pstree -p 3912 nginx(3912)───nginx(19881)
