如何显示 bash 会话的当前进程树?

发布于 2024-11-30 22:23:39 字数 192 浏览 1 评论 0原文

我想创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意中人 2024-12-07 22:23:39

我确信通过一些谷歌搜索,您可以找到如何获取和下载 Mac 版 pstree。但是,您可以使用 psppid 来实现穷人的版本。

例如

ps -eo ppid,pid,cmd | awk '{p[$1]=p[$1]","$3}END{ for(i in p) print i, p[i]}'

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, using ps and ppid.

eg

ps -eo ppid,pid,cmd | awk '{p[$1]=p[$1]","$3}END{ for(i in p) print i, p[i]}'
寄离 2024-12-07 22:23:39

pstree(1) 中支持此功能,方法是使用一个选项仅显示特定 PID 的树并提供当前进程的 PID(Bash 中的 $$),该选项Werner Almesberger 随 Debian 发布的 GPL 许可版本和 Fred Hucht 随 MacOS 发布的 BSD 版本的命名不同。

  • 在 Debian/Ubuntu 上:pstree -s $$

    init────gnome-terminal────bash──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 选项摘要:

    -p pid 仅显示包含进程  的分支
    

这是 MacOS 的别名:

alias psme='pstree -p $'

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 $$

    init───gnome-terminal───bash───pstree
    

    Summary of -s option:

    -s     Show parent processes of the specified process.
    
  • On MacOS: pstree -p $$

    -+= 00001 root /sbin/launchd
     \-+= 25706philipbranning/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
       \-+= 25716 root login -pfl philipbranning /bin/bash -c exec -la bash /bin/bash
         \-+= 25722 philipbranning -bash
           \-+= 32456 philipbranning pstree -p 25722
             \--- 32457 root ps -axwwo user,pid,ppid,pgid,command
    

    Summary of -p option:

    -p pid    show only branches containing process <pid>
    

Here's your alias for MacOS:

alias psme='pstree -p $'
醉生梦死 2024-12-07 22:23:39

我使用的是 Mac OS 10.7 Lion,但我认为这对于其他类 Unix 系统上的类 Bourne shell 来说是相当可移植的。您可能对 ps 参数中的 command 关键字有疑问。

我将以下代码放入名为 procsup.sh 的文件中,该文件定义了一个 shell 函数,用于跟踪进程的父进程直至进程 ID 1。(我经常发现 shell 函数比别名更容易使用。)

procsup()
{
     leaf=$
     ps -eo pid,ppid,command | awk -v leaf="$leaf" \
        '{parent[$1]=$2;command[$1]=$3;}                                                                                                   
     function print_ancestry(pid)                                                                                                          
     {                                                                                                                                     
         print pid " (" command[pid] ") child of " parent[pid];                                                                            
         if(pid!=1) print_ancestry(parent[pid])                                                                                            
     };                                                                                                                                    
     END{\                                                                                                                                 
         print_ancestry(leaf)                                                                                                              
     }'
}

然后我启动了一个 shell 并获取了procsup.sh。在现实生活中,您将确保您的新 shell 在启动时自动获取 procsup.sh,可能在您的个人 .bashrc 中。首先我检查了那个贝壳的祖先。然后我从那个 shell 启动了 vi。像往常一样,与 vi 的交互直到我执行 :shell 后才进入记录。我的终端窗口如下所示:

Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$ . procsup.sh
Mariel:~/Library/Scripts 1j david$ procsup
41926 (-bash) child of 41922
41922 (login) child of 41917
41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281
19281 (/sbin/launchd) child of 1
1 (/sbin/launchd) child of 0
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$ vi

bash-3.2$ # Have just done :shell.
bash-3.2$ . procsup.sh
bash-3.2$ procsup
42325 (/bin/bash) child of 42324
42324 (vi) child of 41926
41926 (-bash) child of 41922
41922 (login) child of 41917
41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281
19281 (/sbin/launchd) child of 1
1 (/sbin/launchd) child of 0
bash-3.2$
bash-3.2$

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.)

procsup()
{
     leaf=$
     ps -eo pid,ppid,command | awk -v leaf="$leaf" \
        '{parent[$1]=$2;command[$1]=$3;}                                                                                                   
     function print_ancestry(pid)                                                                                                          
     {                                                                                                                                     
         print pid " (" command[pid] ") child of " parent[pid];                                                                            
         if(pid!=1) print_ancestry(parent[pid])                                                                                            
     };                                                                                                                                    
     END{\                                                                                                                                 
         print_ancestry(leaf)                                                                                                              
     }'
}

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:

Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$ . procsup.sh
Mariel:~/Library/Scripts 1j david$ procsup
41926 (-bash) child of 41922
41922 (login) child of 41917
41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281
19281 (/sbin/launchd) child of 1
1 (/sbin/launchd) child of 0
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$
Mariel:~/Library/Scripts 1j david$ vi

bash-3.2$ # Have just done :shell.
bash-3.2$ . procsup.sh
bash-3.2$ procsup
42325 (/bin/bash) child of 42324
42324 (vi) child of 41926
41926 (-bash) child of 41922
41922 (login) child of 41917
41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281
19281 (/sbin/launchd) child of 1
1 (/sbin/launchd) child of 0
bash-3.2$
bash-3.2$
彼岸花ソ最美的依靠 2024-12-07 22:23:39

如果您使用像 MacPorts 这样的包管理器,您可以轻松安装 pstree。

If you use a package manager like MacPorts you can easily install pstree.

屌丝范 2024-12-07 22:23:39

我没有您正在寻找的完整答案,但我有一个想法可能会引导您走向正确的方向。该命令

declare -A parent

将创建一个关联数组(如果您使用 Perl,则为散列)

您将需要一些命令来为您提供 PID 和 PPID 的名称-值对...我的猜测是可以使用 mac 的 ps 命令来执行此操作如果你折磨得够多的话。我将像上面一样使用“ps -eo”,但您需要填写空白。

然后你可以做这样的事情:

ps -eo pid,ppid | while read pid ppid
do   
   parent[$pid]=$ppid   
   echo "pid: $pid ppid: ${parent[$pid]} grandppid: ${parent[${parent[$pid]}]}"
done

我在让 $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

declare -A parent

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:

ps -eo pid,ppid | while read pid ppid
do   
   parent[$pid]=$ppid   
   echo "pid: $pid ppid: ${parent[$pid]} grandppid: ${parent[${parent[$pid]}]}"
done

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文