如何显示 bash 会话的当前进程树?
我想创建一个 bash 别名,它为我提供从我正在使用的当前 bash 会话到 init 的进程树。
用例是知道我是否使用了 bash 或 vi 的 :shell
命令。
我使用的是 MacOS X。我听说过 pstree
,但它似乎只显示子进程,而不显示 init 和当前进程之间的关系。
I would like to create a bash alias that gives me the process tree from the current bash session I am using, up to init.
The use case is to know whether I have used bash
or vi
's :shell
command.
I am using MacOS X. I have heard about pstree
, but it seems to only show children, not the relationship between init and the current process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我确信通过一些谷歌搜索,您可以找到如何获取和下载 Mac 版
pstree
。但是,您可以使用ps
和ppid
来实现穷人的版本。例如
I am sure with a a bit of google search, you can find how to get and download
pstree
for the Mac. However, you can do a poor man's version, usingps
andppid
.eg
pstree(1)
中支持此功能,方法是使用一个选项仅显示特定 PID 的树并提供当前进程的 PID(Bash 中的$$
),该选项Werner Almesberger 随 Debian 发布的 GPL 许可版本和 Fred Hucht 随 MacOS 发布的 BSD 版本的命名不同。在 Debian/Ubuntu 上:
pstree -s $$
-s
选项摘要:在 MacOS 上:
pstree -p $$
<预置><代码>-+= 00001 root /sbin/launchd
\-+= 25706philipbranning/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
\-+= 25716 root 登录 -pfl philipbranning /bin/bash -c exec -la bash /bin/bash
\-+= 25722 菲利普布兰宁 -bash
\-+= 32456 菲利普布兰宁 pstree -p 25722
\--- 32457 root ps -axwwo 用户,pid,ppid,pgid,命令
-p
选项摘要:这是 MacOS 的别名:
This is supported in
pstree(1)
by using an option to show the tree only for a particular PID and providing the current process's PID ($$
in Bash), The option is named differently between GPL-licensed version by Werner Almesberger distributed with Debian and the BSD version by Fred Hucht distributed with MacOS.On Debian/Ubuntu:
pstree -s $$
Summary of
-s
option:On MacOS:
pstree -p $$
Summary of
-p
option:Here's your alias for MacOS:
我使用的是 Mac OS 10.7 Lion,但我认为这对于其他类 Unix 系统上的类 Bourne shell 来说是相当可移植的。您可能对 ps 参数中的 command 关键字有疑问。
我将以下代码放入名为 procsup.sh 的文件中,该文件定义了一个 shell 函数,用于跟踪进程的父进程直至进程 ID 1。(我经常发现 shell 函数比别名更容易使用。)
然后我启动了一个 shell 并获取了procsup.sh。在现实生活中,您将确保您的新 shell 在启动时自动获取 procsup.sh,可能在您的个人 .bashrc 中。首先我检查了那个贝壳的祖先。然后我从那个 shell 启动了 vi。像往常一样,与 vi 的交互直到我执行
:shell
后才进入记录。我的终端窗口如下所示:I used Mac OS 10.7 Lion, but I think this will be fairly portable to Bourne-like shells on other Unix-like systems. You may have issues with the command keyword in the argument to ps.
I put the following code in a file named procsup.sh, which defines a shell function to follow the process's parents up to process ID 1. (I often find shell functions easier to work with than aliases.)
Then I started a shell and sourced procsup.sh. In real life you would ensure that your new shells would automatically source procsup.sh when started, maybe in your personal .bashrc. First I checked the ancestry from that shell. Then I started vi from that shell. As usual the interaction with vi didn't make it to the transcript until I did
:shell
. My terminal window looked like this:如果您使用像 MacPorts 这样的包管理器,您可以轻松安装 pstree。
If you use a package manager like MacPorts you can easily install pstree.
我没有您正在寻找的完整答案,但我有一个想法可能会引导您走向正确的方向。该命令
将创建一个关联数组(如果您使用 Perl,则为散列)
您将需要一些命令来为您提供 PID 和 PPID 的名称-值对...我的猜测是可以使用 mac 的 ps 命令来执行此操作如果你折磨得够多的话。我将像上面一样使用“ps -eo”,但您需要填写空白。
然后你可以做这样的事情:
我在让 $parent 的值在 while 循环之外持续存在时遇到了麻烦,否则我会创建第二个 for 循环来从 $$ 遍历回 init。我将把它作为练习留给读者。
I don't have the whole answer that you're looking for, but I've got an idea that might move you in the right direction. The command
will create an associative array (a hash, if you speak Perl)
You will need some command that will give you name-value pairs for PID and PPID... my guess is that the mac's ps command can be made to do this if you torture it enough. I'm going to use 'ps -eo' as above, but you'll want to fill in the blanks.
Then you can do something like this:
I was having trouble making the values of $parent persist outside of my while loop, otherwise I would have created a second for loop to traverse from $$ back to init. I'll leave that as an exercise to the reader.