linux pstree,初学者的Linux pstree命令教程(8个示例)

初学者的Linux pstree命令教程(8个示例)

由于Linux系统中的每个进程(除了第一个进程之外)都有一个父进程,因此有时候可以更容易理解是否所有进程都以树结构显示。 你会很高兴知道存在一个命令行实用程序 - 被称为pstree - 显示进程树。

在本教程中,我们将使用一些易于理解的示例来讨论pstree命令的基础知识。 但在这之前,值得一提的是,这里的所有示例都已在Ubuntu 16.04 LTS机器上进行了测试。

Linux pstree命令

如开头所述,pstree命令显示进程树。 以下是它的语法:

pstree [options]

以下是该工具的手册页中提到的内容:pstree shows running processes as a tree.  The tree is rooted at either

pid  or  init  if  pid  is  omitted.   If a user name is specified, all

process trees rooted at processes owned by that user are shown.

pstree visually merges identical branches by  putting  them  in  square

brackets and prefixing them with the repetition count, e.g.

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 hap?

pened.  This is useful for when pstree is run in a 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.

以下是一些Q&A样式的示例,应该给你一个关于pstree命令如何工作的好主意:

Q1。 如何使用pstree命令?

基本用法很简单:您只需执行'pstree'sans任何选项。

pstree

02a929550666c2fd1b288872eb40326c.png

所以你可以看到这是pstree如何在输出中产生与进程相关的信息。

Q2。 如何使pstree在输出中包含命令行参数?

这可以使用-a命令行选项完成。

pstree -a

98754b9fce55e116a75e5b2be1b5d95f.png

所以你可以看到pstree命令现在也显示了一些进程的命令行选项。

Q3。 如何强制pstree在输出中展开相同的子树?

默认情况下,pstree命令通过将它们放在方括号中并将它们加上重复计数前缀来合并相同的分支。 像这样的东西:

06591c22dd623bc10ad91b1fea99bac3.png

但是,如果您愿意,可以强制该工具展开相同的树,您可以使用-c命令行选项进行操作。

pstree -c

88743b36f5e303fa9c97c4851b8322db.png

Q4。 如何让pstree突出特定的过程?

如果您希望该工具突出显示输出中的特定进程,请使用-H命令行选项。

pstree -H [PID]

PID是您想要突出显示的流程的ID。 例如,我使用以下命令在我的系统上突出显示了firefox进程:

pstree -H 3124

640b999dbd082013ecce12f42d452032.png

所以你可以看到'firefox'这个名字与其他名字相比有点突出。

Q5。 如何让pstree在输出中显示进程组ID?

为此,请使用-g命令行选项。

pstree -g

9cfa5a859069bc90019f60ae6e26895b.png

因此,您可以看到每个进程名称后面的进程组标识在括号中显示为十进制数字。

Q6。 如何根据PID进行pstree排序过程?

默认情况下,pstree按名称排序具有相同祖先的进程。 但是,如果需要,也可以使用PID进行pstree排序,也可以使用-n命令行选项进行排序。

pstree -n

请注意,这种排序也称为数字排序。

Q7。 如何使pstree显示特定于用户的进程树?

如果您希望pstree显示以特定用户拥有的进程为根的所有进程树,那么您只需将该用户的名称作为输入传递给该命令。

例如,

pstree himanshu

所以在这种情况下,我的系统上产生了以下输出:

d408b421a8ef3dedb273f262f45d814c.png

Q8。 如何限制Pstree到一个特定的过程?

如果您希望pstree仅显示特定进程的父级和子级信息,请使用-s选项。

pstree -s [PID]

例如,我想限制Pstree输出到我系统上的firefox进程,所以我执行了下面的命令:

pstree -s 3124

这里是工具显示的输出:

a06158f109c14f413d187e6ba660cd87.png

结论

根据您所做的工作类型,pstree命令可以提供很大的帮助。 在本教程中,我们已经讨论了该工具提供的一些关键命令行选项。 有关更多信息,请转至其手册页 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值