02-查看进程的树型结构:pstree

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)

 

posted @ 2019-06-05 15:05  hejp  阅读(1053)  评论(0编辑  收藏  举报