我如何使用bash指定进程的顶级父PID? [英] How do I find the top-level parent PID of a given process using bash?
问题描述
比方说,我跑 PS AXF
,我可以看到我的命令的进程树看起来是这样的:
Let's say I run ps axf
and I can see that my command's process tree looks like this:
800 ? Ss 0:00 /usr/sbin/sshd
10186 ? Ss 0:00 \_ sshd: yukondude [priv]
10251 ? S 0:00 \_ sshd: yukondude@pts/0
10252 pts/0 Ss 0:00 \_ -bash
10778 pts/0 S 0:00 \_ su -
10785 pts/0 S 0:00 \_ -su
11945 pts/0 R+ 0:00 \_ ps axf
我知道我可以检查 $
当前shell的PID(10785)或 $ PPID
父PID(10778)。
I know I can check $$
for the current shell's PID (10785) or $PPID
for the parent PID (10778).
但我只想顶级父PID,这将是在这个例子中800(SSH守护进程)。有没有办法做到这一点容易吗?
But I just want the top-level parent PID, which would be 800 (SSH daemon) in this example. Is there any way to do that easily?
我从<有学问href=\"http://stackoverflow.com/questions/1525605/linux-programmatically-get-parent-pid-of-another-process/1525673#1525673\">this SO回答,我可以递归查询在第4项的/ proc / PID / STAT
文件中查找每个进程的父PID:
I learned from this SO answer that I can recursively check the 4th entry in the /proc/PID/stat
file to find each process's parent PID:
# cut -f4 -d' ' /proc/10785/stat
10778
# cut -f4 -d' ' /proc/10778/stat
10252
# cut -f4 -d' ' /proc/10252/stat
10251
# cut -f4 -d' ' /proc/10251/stat
10186
# cut -f4 -d' ' /proc/10186/stat
800
# cut -f4 -d' ' /proc/800/stat
1
(顶层父PID将是我到达之前一的init
的PID,即,1)
在我写一个小环(我甚至不知道是否可以使用在bash递归),要做到这一点,有没有我缺少一个更简单的方法是什么?也许只是一个在的/ proc
文件的参数? A 的grep
通过这些文件并没有透露任何明显。
Before I write a little loop (I'm not even sure if you can use recursion in bash) to do this, is there a much more straightforward method that I'm missing? Maybe just another parameter of a file under /proc
? A grep
through those files didn't reveal anything obvious.
修改:当然,对于所有Linux进程顶级工艺/ sbin目录/初始化为1。PID我要的是家长之前只是的PID:倒数第二家长。
Edit: Of course, the top-level process for all Linux processes is /sbin/init with a PID of 1. What I want is the PID of the parent just before that: the penultimate parent.
推荐答案
失败一个更好的解决方案,这里有一个简单的(递归)脚本,让你给它的进程号的顶级父PID(或当前shell,如果你离开了PID参数):
Failing a better solution, here's a simple (recursive) script to get the top-level parent PID of any process number you give it (or the current shell if you leave out the PID argument):
#!/bin/bash
# Look up the top-level parent Process ID (PID) of the given PID, or the current
# process if unspecified.
function top_level_parent_pid {
# Look up the parent of the given PID.
pid=${1:-$$}
stat=($(</proc/${pid}/stat))
ppid=${stat[3]}
# /sbin/init always has a PID of 1, so if you reach that, the current PID is
# the top-level parent. Otherwise, keep looking.
if [[ ${ppid} -eq 1 ]] ; then
echo ${pid}
else
top_level_parent_pid ${ppid}
fi
}
只是源
这个脚本,并调用 top_level_parent_pid
带或不带PID参数,如合适的。
Just source
this script and call top_level_parent_pid
with or without a PID argument, as appropriate.
感谢@Dennis威廉姆森为他如何紧凑,有效地写这个剧本的许多建议。
Thanks to @Dennis Williamson for his many suggestions on how to write this script compactly and efficiently.
这篇关于我如何使用bash指定进程的顶级父PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!