現在使用しているbashセッションからinitまでのプロセスツリーを提供するbashエイリアスを作成したいと思います。
ユースケースは、私がまたはのコマンドを使用したbash
かどうかを知ることです。vi
:shell
私はMacOSXを使用しています。聞いpstree
たことはありますが、initと現在のプロセスの関係ではなく、子のみが表示されているようです。
pstree
ちょっとしたグーグル検索で、Mac用に入手してダウンロードする方法を見つけることができると確信しています。ps
ただし、とを使用して、貧乏人のバージョンを実行できますppid
。
例えば
ps -eo ppid,pid,cmd | awk '{p[$1]=p[$1]","$3}END{ for(i in p) print i, p[i]}'
これはpstree(1)
、特定のPIDに対してのみツリーを表示するオプションを使用し、現在のプロセスのPID($$
Bash)を提供することでサポートされます。このオプションの名前は、Debianで配布されているWernerAlmesbergerによるGPLライセンスバージョンとFredによるBSDバージョンで異なります。 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 $$'
私はMacOS10.7 Lionを使用しましたが、これは他のUnixライクなシステムのBourneライクなシェルにかなり移植可能だと思います。psの引数のcommandキーワードに問題がある可能性があります。
次のコードをprocsup.shという名前のファイルに入れました。このファイルは、プロセスの親をプロセスID 1まで追跡するシェル関数を定義しています(シェル関数はエイリアスよりも操作しやすいことがよくあります)。
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)
}'
}
次に、シェルを起動してprocsup.shを調達しました。実際には、新しいシェルが開始時に、おそらく個人の.bashrcでprocsup.shを自動的にソースするようにします。まず、そのシェルから祖先を確認しました。それから私はそのシェルから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$
MacPortsのようなパッケージマネージャーを使用している場合は、pstreeを簡単にインストールできます。
私はあなたが探している完全な答えを持っていませんが、私はあなたを正しい方向に動かすかもしれないという考えを持っています。コマンド
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ループの外で永続化するのに問題がありました。そうしないと、$$からinitに戻るための2番目のforループを作成することになります。それは読者の練習問題として残しておきます。