如何使用bash找到给定进程的顶级父PID?

假设我运行ps axf,我可以看到我的命令的进程树如下所示:
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)。

但是,我只想要顶级的父级PID,在这个例子中它是800(SSH守护进程)。有什么办法吗?

this SO answer学到,我可以递归地检查/ proc / PID / stat文件中的第4个条目来查找每个进程的父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之前的一个PID,即1.)

在我写一个循环之前(我甚至不知道如果你可以在bash中使用递归)来做到这一点,是否有一个更简单的方法,我失踪了?也许只是/ proc下的一个文件的另一个参数?通过这些文件的grep没有显示任何明显的东西。

编辑:当然,所有Linux进程的顶级进程是/ sbin / init,PID为1.我想要的是父对象之前的PID:倒数第二个父进程。

没有一个更好的解决方案,这是一个简单的(递归)脚本,以获得您给出的任何进程号的顶级父PID(或当前shell如果省略PID参数):
#!/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
}

只需输入此脚本,并根据需要调用带或不带PID参数的top_level_parent_pid。

感谢@Dennis Williamson对于如何紧凑而有效地编写这个脚本的许多建议。

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...