
0 前言
在
Linux shell编程学习笔记59: ps 获取系统进程信息,类似于Windows系统中的tasklist 命令 https://blog.csdn.net/Purpleendurer/article/details/139696466?spm=1001.2014.3001.5501
https://blog.csdn.net/Purpleendurer/article/details/139696466?spm=1001.2014.3001.5501
 
中我们研究了ps命令。在Linux中,通过ps命令,我们可以查看系统中运行的进程信息,包括进程的ID、父进程ID、CPU和内存占用等信息。不过ps命令显示的进程信息中,进程间的父子关系还不够直观。
Linux还提供了pstree命令,可以以树的形式直观地显示进程间的关系。
1 pstree 命令 的功能、格式和选项说明
我们先看一下pstree命令的帮助信息。
pstree命令 不支持 --help选项。
[purpleendurer @ bash ~] pstree --help
pstree: 未知的选项 `--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     show         SELinux security contextsPID    start at this PID; default is 1 (init)USER   show only trees rooted at processes of this user[purpleendurer @ bash ~] 
 
 
1.1 pstree 命令 的功能
显示进程树(Display a tree of processes)。
1.2 pstree命令的格式
ps [选项] [PID | USER]
或
ps -V
1.3 pstree命令的选项
| 选项 | 功能 | 
|---|---|
| -a --arguments | 显示进程对应命令行有参数,如果是被记忆体置换出去的进程则会加上括号 | 
| -A --ascii | 使用ASCII码线条绘制字符 | 
| -c --compact | 如果有重复的进程名, 则分开列出(预设值是会在前面加上 *) | 
| -h --highlight-all | 突出显示当前流程及其祖先 | 
| -H PID --highlight-pid=PID | 突出显示指定PID的过程及其祖先 | 
| -g --show-pgids | 显示进程组 ID;隐含 -c | 
| -G --vt100 | 使用 VT100 线条绘制字符 | 
| -l --long | 不要截断长线 | 
| -n --numeric-sort | 按 PID 对输出进行排序 | 
| -N type --ns-sort=type | 按命名空间类型排序(IPC、MNT、NET、PID、USER、UTS) | 
| -p --show-pids | 显示 PID;隐含 -c | 
| -s --show-parents | 显示所选进程的父进程 | 
| -S --ns-changes | 显示命名空间转换 | 
| -u --uid-changes | 显示 UID 转换 | 
| -U --unicode | 使用 UTF-8 (Unicode) 线条绘制字符 | 
| -V --version | 显示版本信息 | 
1.3 pstree命令的参数
| 参数 | 说明 | 
|---|---|
| PID | 从指定的PID开始,默认值为1 | 
| USER | 以指定用户显示进程树 | 
2 pstree命令实列
2.1 pstree | more 分页显示进程树
[purpleendurer @ bash ~] pstree | more
systemd-+-ETSMJBZGN0---3*[{ETSMJBZGN0}]|-ETSMJBZGN10|-ETSMJBZUI1-+-29*[QOperationLogTh]|            |-11*[QRemoveLabelThr]|            |-{QFunctionPipe}|            |-{QKeepSessionThr}|            |-2*[{QOperationLogTh}]|            |-{QPluginOutPipe}|            |-{QProcessManager}|            |-{QRightMenuPipe}|            `-{QSLPluginPipe}|-ETSMJBZUI11---8*[{ThreadServer}]|-ETSMJBZUI13---sleep|-ETSMJBZWJTX0---{ETSMJBZWJTX0}|-2*[ETSMJBZWJTX1]|-FoxitOfficeSuit---FoxitOfficeSuit-+-FoxitUpgrade|                                   `-3*[{FoxitOfficeSuit}]|-NetworkManager-+-dhclient|                |-dnsmasq|                |-{gdbus}|                `-{gmain}|-PNXClientHost.o---PNXClientHost.o|-SuwellUpdateSer|-WLHXXYCGN701|-WLHXXYCGN702---{WLHXXYCGN702}|-WLHXXYCGN802|-accounts-daemon-+-{gdbus}|                 `-{gmain}|-acpid|-activation-daem|-2*[agetty]|-aptd---{gmain}|-auditd---{auditd}|-avahi-daemon---avahi-daemon|-backup-daemon---{QDBusConnection}|-biometric-authe-+-2*[{biometric-authe}]|                 |-{gdbus}|                 `-{gmain}|-2*[cat]
--更多--
 
2.2 pstree -apn | more :用ASCII码线条绘制字符显示在运行的进程树,包括命令参数和PID
[purpleendurer @ bash ~] pstree -apn | more
systemd,1 splash|-sqaxsafeforcnos,367 --minishow|   |-{QXcbEventReader},371|   |-{llvmpipe-0},386|   |-{llvmpipe-1},387|   |-{llvmpipe-2},388|   |-{llvmpipe-3},389|   |-{QDBusConnection},390|   |-{gmain},415|   |-{gdbus},416|   |-{CASocketThread},428|   |-{sqaxsafeforcnos},432|   |-{sqaxsafeforcnos},433|   |-{sqaxsafeforcnos},434|   |-{sqaxsafeforcnos},435|   |-{sqaxsafeforcnos},436|   |-{sqaxsafeforcnos},437|   |-{sqaxsafeforcnos},438|   |-{sqaxsafeforcnos},439|   |-{sqaxsafeforcnos},497|   |-{sqaxsafeforcnos},498|   |-{sqaxsafeforcnos},499|   |-{sqaxsafeforcnos},500|   |-{sqaxsafeforcnos},501|   |-{sqaxsafeforcnos},502|   |-{sqaxsafeforcnos},503|   |-{sqaxsafeforcnos},504|   |-{sqaxsafeforcnos},506|   |-{sqaxsafeforcnos},507|   |-{sqaxsafeforcnos},508|   |-{sqaxsafeforcnos},509|   |-{sqaxsafeforcnos},510|   |-{sqaxsafeforcnos},511|   |-{sqaxsafeforcnos},512|   |-{sqaxsafeforcnos},516|   |-{XTableThread},520|   |-{SpeedCtrl},522|   |-{QThread},524|   `-{CpuMemState},525|-systemd-udevd,420|-haveged,423 --Foreground --verbose=1 -w 1024|-systemd-journal,425|-qaxtray_5,518 --reboot|   |-{QXcbEventReader},523|   |-{llvmpipe-0},526|   |-{llvmpipe-1},527|   |-{llvmpipe-2},528|   |-{llvmpipe-3},529|   |-{QDBusConnection},530|   |-{gmain},531|   |-{gdbus},532|   |-{qaxtray_5},581|   |-{qaxtray_5},582|   |-{qaxtray_5},583|   |-{qaxtray_5},584|   |-{QThread},591
--更多--

3.3 pstree -p 795:显示以PID为795的进程为根的进程树,包括PID
[purpleendurer @ bash ~] pstree -p 795
accounts-daemon(795)─┬─{gdbus}(834)└─{gmain}(824)
[purpleendurer @ bash ~] 
