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

问题描述 投票:6回答:5

我想创建一个bash别名,它给我从我正在使用的当前bash会话的进程树,直到init。

用例是知道我是否使用过bashvi:shell命令。

我正在使用MacOS X.我听说过pstree,但它似乎只显示孩子,而不是init和当前进程之间的关系。

bash process vi ps
5个回答
5
投票

我肯定通过谷歌搜索,您可以找到如何获取并下载适用于Mac的pstree。但是,你可以使用psppid做一个穷人的版本。

例如

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

3
投票

pstree(1)支持这一点,它使用一个选项只显示特定PID的树并提供当前进程的PID(Bash中的$$),该选项在与Debian和BSD版本一起发布的Werner Almesberger的GPL许可版本之间有不同的名称。 Fred Hucht与MacOS一起发行。

  • 在Debian / Ubuntu:pstree -s $$ init───gnome-terminal───bash───pstree -s选项摘要: -s Show parent processes of the specified process.
  • 在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 -p选项摘要: -p pid show only branches containing process <pid>

这是MacOS的别名:

alias psme='pstree -p $$'

2
投票

我使用的是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的祖先。然后我从那个shell开始了vi。像往常一样,在我做:shell之前,与vi的相互作用并没有成为成绩单。我的终端窗口看起来像这样:

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$

1
投票

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


1
投票

我没有你想要的全部答案,但我有一个想法可能会让你朝着正确的方向前进。命令

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。我将把它作为练习留给读者。

© www.soinside.com 2019 - 2024. All rights reserved.