pstree进程数命令
pstree
是一个用于显示系统中进程层次结构的命令行工具,它以树状图的形式展示进程之间的父子关系。
基本语法
pstree [选项] [用户名或PID]
- 不带任何参数运行
pstree
,将显示当前用户的进程树。 - 可以指定用户名或进程 ID (PID) 来查看特定进程树。 (pstree root)
pstree args
root@vgt-ubuntu-10:~# pstree --help
pstree: unrecognized option '--help'
Usage: pstree [-acglpsStTuZ] [ -h | -H PID ] [ -n | -N type ]
[ -A | -G | -U ] [ PID | USER ]
or: pstree -V
Display a tree of processes. ### 显示进程树
-a, --arguments show command line arguments ### 命令行输出
-A, --ascii use ASCII line drawing characters
-c, --compact-not don't compact identical subtrees
-C, --color=TYPE color process by attribute
(age)
-g, --show-pgids show process group ids; implies -c ### 以组形式显示
-G, --vt100 use VT100 line drawing characters
-h, --highlight-all highlight current process and its ancestors ### 当前进程高亮显示
-H PID, --highlight-pid=PID
highlight this process and its ancestors
-l, --long don't truncate long lines
-n, --numeric-sort sort output by PID
-N TYPE, --ns-sort=TYPE
sort output by this namespace type
(cgroup, ipc, mnt, net, pid, time, user, uts)
-p, --show-pids show PIDs; implies -c ### 显示pid
-s, --show-parents show parents of the selected process
-S, --ns-changes show namespace transitions
-t, --thread-names show full thread names ### 显示线程名字
-T, --hide-threads hide threads, show only processes
-u, --uid-changes show uid transitions
-U, --unicode use UTF-8 (Unicode) line drawing characters
-V, --version display version information
-Z, --security-context
show security attributes
PID start at this PID; default is 1 (init)
USER show only trees rooted at processes of this user
常用参数
以下是 pstree
的主要选项及其功能:
-a
:显示完整命令行参数- 显示每个进程的完整命令行参数,而不仅仅是进程名称。
- 示例:
pstree -a
输出示例:
systemd –switched-root –system –deserialize 21
├─sshd -D
│ └─sshd
│ └─bash
-p
:显示进程 ID (PID)- 在每个进程名称后显示其 PID。
- 示例:
pstree -p
输出示例:
systemd(1)─┬─sshd(1234)───sshd(5678)───bash(5679)
└─nginx(2345)
-u
:显示用户转换- 如果进程的所有者与父进程不同,显示用户名。
- 示例:
pstree -u
输出示例:
systemd─┬─sshd(root)───sshd(user1)───bash
└─nginx(root)
-h
:高亮当前进程- 高亮显示当前进程及其祖先进程(通常用于终端中运行的进程)。
- 示例:
pstree -h
-l
:长格式显示- 避免截断长进程名称,适合显示完整名称。
- 示例:
pstree -l
-n
:按 PID 排序- 按照进程 ID 的数值顺序排序,而不是默认的字母顺序。
- 示例:
pstree -n -p
-c
:紧凑模式- 禁用重复进程的合并显示(默认情况下,相同的子进程会被压缩显示为
{进程名}
)。 - 示例:
pstree -c
默认输出(合并):
nginx───2*[nginx] ### 2* 两个nginx线程
使用-c
输出:
nginx─┬─nginx
└─nginx
- 禁用重复进程的合并显示(默认情况下,相同的子进程会被压缩显示为
-g
:显示进程组 ID (PGID)- 在每个进程后显示其进程组 ID。
- 示例:
pstree -g
输出示例:
systemd(1)─┬─sshd(1234,1234)───bash(5678,5678)
└─nginx(2345,2345)
-A
/-U
/-G
:调整树状图样式-A
:使用 ASCII 字符绘制树(默认)。-U
:使用 Unicode 字符绘制树。-G
:使用 VT100 字符绘制树。- 示例:
pstree -U
-s
:显示特定进程的父进程- 显示指定进程的完整父进程链。
- 示例:
pstree -s 5678
输出示例:
systemd───sshd───sshd───bash
用户名
或PID
:指定目标- 显示特定用户或 PID 的进程树。
- 示例:
pstree root
或:
pstree 1234
组合示例
显示所有用户进程、PID 和完整命令行:
pstree -a -p
输出示例:
systemd(1) –switched-root –system
├─sshd(1234) -D
│ └─sshd(5678)
│ └─bash(5679)
└─nginx(2345) -g daemon off;高亮当前进程并显示 PID:
pstree -h -p显示特定 PID 的父进程链:
pstree -s -p 5679
输出示例:
systemd(1)───sshd(1234)───sshd(5678)───bash(5679)
4.显示线程pid及uid变化
pstree -pu
注意事项
安装:如果系统中没有
pstree
,需要安装。例如在 Ubuntu 上:
sudo apt install psmisc输出过长:进程树较复杂时,可以结合
less
或重定向到文件:
pstree -a -p | less
或:
pstree -a -p > processes.txt权限:某些进程信息可能需要 root 权限才能完整显示。