pstree (以树状图格式显示进程)

瑞兹 发表于 2020-11-16 09:06
浏览次数:
在手机上阅读

在类似Unix的操作系统上,pstree命令以树格式显示进程。

查看英文版

目录

1 pstree 运行系统环境

2 pstree 描述

3 pstree 语法

4 pstree 例子

pstree 运行系统环境

Linux

pstree 描述

如果指定了用户名,则会显示所有以该用户拥有的进程为根的进程树。

通过将相同的分支放在方括号中并在其前面加上重复计数来合并它们,例如:

init-+-getty
     |-getty
     |-getty
     `-getty

变成

init---4*[getty]

进程的子线程位于父进程下,并以大括号显示该进程的名称,例如,

icecast2---13*[{icecast2}]

如果将pstree称为pstree.x11,它将提示用户在行尾按回车键,直到发生这种情况才返回。当pstree在xterminal中运行时,执行此命令很有用。

某些内核或安装参数(例如procfs的hidepid选项)将隐藏某些进程的信息。在这些情况下,pstree将尝试在没有此信息的情况下构建树,将进程名称显示为问号。

The pstree command shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a username is specified, all process trees rooted at processes owned by that user are shown.

Identical branches are merged by putting them in square brackets and prefixing them with the repetition count, for example:

init-+-getty
     |-getty
     |-getty
     `-getty

becomes

init---4*[getty]

Child threads of a process are found under the parent process and are shown with the process name in curly braces, e.g.,

icecast2---13*[{icecast2}]

If pstree is called as pstree.x11 then it will prompt the user at the end of the line to press return and will not return until that has happened. Doing this command is useful for when pstree is run in an xterminal.

Certain kernel or mount parameters, such as the hidepid option for procfs, will hide information for some processes. In these situations pstree will attempt to build the tree without this information, showing process names as question marks.

查看英文版

查看中文版

pstree 语法

pstree [-a, --arguments] [-c, --compact] 
       [-h, --highlight-all, -Hpid, --highlight-pid pid] [-g] --show-pgids] 
       [-l, --long] [-n, --numeric-sort] [-p, --show-pids] [-s, --show-parents] 
       [-u, --uid-changes] [-Z, --security-context] 
       [-A, --ascii, -G, --vt100, -U, --unicode] [pid, user]
pstree -V, --version

选件

-a 显示命令行 参数。如果将某个进程的命令行换出,该进程将显示在括号中。-a隐式禁用进程压缩,但不禁用线程。
-A 使用ASCII 字符绘制树。
-c 禁用相同子树的压缩。默认情况下,子树会尽可能压缩。
-G 使用VT100线条图字符。
-h 突出显示当前的过程及其祖先。如果终端不支持突出显示,或者当前进程或其任何祖先都不在显示的子树中,则此开关为无操作。
-H -h类似,但突出显示指定的进程。不像-Hpstree使用时出现故障-H如果高亮不可用。
-g 显示PGID。进程组ID在每个进程名称后的括号中显示为十进制数字。-p隐式禁用压缩。如果同时显示PID和PGID,则首先显示PID。
-l 显示长行。默认情况下,如果输出发送到非tty或显示宽度未知,则将行截断为显示宽度或132 。
-n 使用PID而不是名称对具有相同祖先的进程进行排序。(数字排序。)
-p 显示PID。PID在每个过程名称后的括号中显示为十进制数字。-p隐式禁用压缩。
-s 显示指定进程的父进程。
-u 显示uid转换。每当进程的uid与它的父进程的uid不同时,新uid就会在进程名称后的括号中显示。
-U 使用UTF-8(Unicode)线描字符。在Linux 1.1-54及更高版本中,在控制台上以echo -e'33 %8'进入UTF-8模式,并以echo -e'33 %@'离开
-V 显示版本信息。
-Z (SELinux)显示每个进程的安全上下文。仅当pstree与SELinux支持兼容时,此标志才有效。
pstree [-a, --arguments] [-c, --compact] 
       [-h, --highlight-all, -Hpid, --highlight-pid pid] [-g] --show-pgids] 
       [-l, --long] [-n, --numeric-sort] [-p, --show-pids] [-s, --show-parents] 
       [-u, --uid-changes] [-Z, --security-context] 
       [-A, --ascii, -G, --vt100, -U, --unicode] [pid, user]
pstree -V, --version

Options

-a Show command line arguments. If the command line of a process is swapped out, that process is shown in parentheses. -a implicitly disables compaction for processes but not threads.
-A Use ASCII characters to draw the tree.
-c Disable compaction of identical subtrees. By default, subtrees are compacted whenever possible.
-G Use VT100 line drawing characters.
-h Highlight the current process and its ancestors. This switch is a no-op if the terminal doesn't support highlighting or if neither the current process nor any of its ancestors are in the subtree being shown.
-H Like -h, but highlight the specified process instead. Unlike with -hpstree fails when using -H if highlighting is not available.
-g Show PGIDs. Process Group IDs are shown as decimal numbers in parentheses after each process name. -p implicitly disables compaction. If both PIDs and PGIDs are displayed then PIDs are shown first.
-l Display long lines. By default, lines are truncated to the display width or 132 if output is sent to a non-tty or if the display width is unknown.
-n Sort processes with the same ancestor by PID instead of by name. (Numeric sort.)
-p Show PIDs. PIDs are shown as decimal numbers in parentheses after each process name. -p implicitly disables compaction.
-s Show parent processes of the specified process.
-u Show uid transitions. Whenever the uid of a process differs from the uid of its parent, the new uid is shown in parentheses after the process name.
-U Use UTF-8 (Unicode) line drawing characters. Under Linux 1.1-54 and above, UTF-8 mode is entered on the console with echo -e ' 33%8' and left with echo -e ' 33%@'
-V Display version information.
-Z (SELinux) Show security context for each process. This flag will only work if pstree is compilied with SELinux support.

查看英文版

查看中文版

pstree 例子

pstree -h

将所有流程显示为树,突出显示当前流程及其祖先。输出将类似于以下内容:

init─┬─NetworkManager─┬─dhclient
     │                ├─dnsmasq
     │                └─3*[{NetworkManager}]
     ├─accounts-daemon───2*[{accounts-daemon}]
     ├─atd
     ├─avahi-daemon───avahi-daemon
     ├─bluetoothd
     ├─console-kit-dae───63*[{console-kit-dae}]
     ├─cron
     ├─cupsd
     ├─dbus-daemon
     ├─dd
     ├─6*[getty]
     ├─klogd
     ├─lightdm─┬─Xorg
     │         ├─lightdm─┬─init─┬─apache2───2*[apache2───26*[{apache2}]]
     │         │         │      ├─at-spi-bus-laun─┬─dbus-daemon
     │         │         │      │                 └─3*[{at-spi-bus-laun}]
     │         │         │      ├─at-spi2-registr───{at-spi2-registr}
     │         │         │      ├─dbus-daemon
     │         │         │      ├─dropbox───29*[{dropbox}]
     │         │         │      ├─gconfd-2
     │         │         │      ├─gimp─┬─script-fu
     │         │         │      │      └─2*[{gimp}]
     │         │         │      ├─gnome-terminal─┬─2*[bash───vim]
     │         │         │      │                ├─bash───pstree
     │         │         │      │                ├─gnome-pty-helpe
     │         │         │      │                └─3*[{gnome-terminal}]
     │         │         │      ├─gvfs-afc-volume───2*[{gvfs-afc-volume}]
     │         │         │      ├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
     │         │         │      ├─gvfs-mtp-volume───{gvfs-mtp-volume}
     │         │         │      ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
     │         │         │      ├─gvfsd───{gvfsd}
     │         │         │      ├─gvfsd-fuse───4*[{gvfsd-fuse}]
     │         │         │      ├─gvfsd-metadata───{gvfsd-metadata}
     │         │         │      ├─indicator-appli───{indicator-appli}
     │         │         │      ├─lxsession─┬─lxpanel───2*[{lxpanel}]
     │         │         │      │           ├─nm-applet───2*[{nm-applet}]
     │         │         │      │           ├─openbox───{openbox}
     │         │         │      │           ├─pcmanfm───2*[{pcmanfm}]
     │         │         │      │           └─2*[{lxsession}]
     │         │         │      ├─menu-cached───2*[{menu-cached}]
     │         │         │      ├─ssh-agent
     │         │         │      ├─2*[upstart-dbus-br]
     │         │         │      ├─upstart-event-b
     │         │         │      ├─upstart-file-br
     │         │         │      ├─xclip
     │         │         │      ├─xfce4-notifyd
     │         │         │      └─xfconfd
     │         │         └─{lightdm}
     │         └─2*[{lightdm}]
     ├─modem-manager
     ├─mysqld───16*[{mysqld}]
     ├─ntpd
     ├─packagekitd───2*[{packagekitd}]
     ├─polkitd───2*[{polkitd}]
     ├─sshd
     ├─syslogd
     ├─systemd-logind
     ├─systemd-udevd
     ├─udisksd───4*[{udisksd}]
     ├─upstart-file-br
     ├─upstart-socket-
     └─upstart-udev-br
pstree -h

Display all processes as a tree, with the current process and its ancestors highlighted. Output will resemble the following:

init─┬─NetworkManager─┬─dhclient
     │                ├─dnsmasq
     │                └─3*[{NetworkManager}]
     ├─accounts-daemon───2*[{accounts-daemon}]
     ├─atd
     ├─avahi-daemon───avahi-daemon
     ├─bluetoothd
     ├─console-kit-dae───63*[{console-kit-dae}]
     ├─cron
     ├─cupsd
     ├─dbus-daemon
     ├─dd
     ├─6*[getty]
     ├─klogd
     ├─lightdm─┬─Xorg
     │         ├─lightdm─┬─init─┬─apache2───2*[apache2───26*[{apache2}]]
     │         │         │      ├─at-spi-bus-laun─┬─dbus-daemon
     │         │         │      │                 └─3*[{at-spi-bus-laun}]
     │         │         │      ├─at-spi2-registr───{at-spi2-registr}
     │         │         │      ├─dbus-daemon
     │         │         │      ├─dropbox───29*[{dropbox}]
     │         │         │      ├─gconfd-2
     │         │         │      ├─gimp─┬─script-fu
     │         │         │      │      └─2*[{gimp}]
     │         │         │      ├─gnome-terminal─┬─2*[bash───vim]
     │         │         │      │                ├─bash───pstree
     │         │         │      │                ├─gnome-pty-helpe
     │         │         │      │                └─3*[{gnome-terminal}]
     │         │         │      ├─gvfs-afc-volume───2*[{gvfs-afc-volume}]
     │         │         │      ├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
     │         │         │      ├─gvfs-mtp-volume───{gvfs-mtp-volume}
     │         │         │      ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
     │         │         │      ├─gvfsd───{gvfsd}
     │         │         │      ├─gvfsd-fuse───4*[{gvfsd-fuse}]
     │         │         │      ├─gvfsd-metadata───{gvfsd-metadata}
     │         │         │      ├─indicator-appli───{indicator-appli}
     │         │         │      ├─lxsession─┬─lxpanel───2*[{lxpanel}]
     │         │         │      │           ├─nm-applet───2*[{nm-applet}]
     │         │         │      │           ├─openbox───{openbox}
     │         │         │      │           ├─pcmanfm───2*[{pcmanfm}]
     │         │         │      │           └─2*[{lxsession}]
     │         │         │      ├─menu-cached───2*[{menu-cached}]
     │         │         │      ├─ssh-agent
     │         │         │      ├─2*[upstart-dbus-br]
     │         │         │      ├─upstart-event-b
     │         │         │      ├─upstart-file-br
     │         │         │      ├─xclip
     │         │         │      ├─xfce4-notifyd
     │         │         │      └─xfconfd
     │         │         └─{lightdm}
     │         └─2*[{lightdm}]
     ├─modem-manager
     ├─mysqld───16*[{mysqld}]
     ├─ntpd
     ├─packagekitd───2*[{packagekitd}]
     ├─polkitd───2*[{polkitd}]
     ├─sshd
     ├─syslogd
     ├─systemd-logind
     ├─systemd-udevd
     ├─udisksd───4*[{udisksd}]
     ├─upstart-file-br
     ├─upstart-socket-
     └─upstart-udev-br

查看英文版

查看中文版

其他命令行

pack | pagesize | parted | partprobe | paste | passwd | pax | pcat | pg | perl | pgrep | pico | pine | ping | pr | printenv | priocntl | printf | pvs | pwd |

如此好文,分享给朋友