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

前端之家收集整理的这篇文章主要介绍了如何使用bash找到给定进程的顶级父PID?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我运行ps axf,我可以看到我的命令的进程树如下所示:
  1. 800 ? Ss 0:00 /usr/sbin/sshd
  2. 10186 ? Ss 0:00 \_ sshd: yukondude [priv]
  3. 10251 ? S 0:00 \_ sshd: yukondude@pts/0
  4. 10252 pts/0 Ss 0:00 \_ -bash
  5. 10778 pts/0 S 0:00 \_ su -
  6. 10785 pts/0 S 0:00 \_ -su
  7. 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:

  1. # cut -f4 -d' ' /proc/10785/stat
  2. 10778
  3. # cut -f4 -d' ' /proc/10778/stat
  4. 10252
  5. # cut -f4 -d' ' /proc/10252/stat
  6. 10251
  7. # cut -f4 -d' ' /proc/10251/stat
  8. 10186
  9. # cut -f4 -d' ' /proc/10186/stat
  10. 800
  11. # cut -f4 -d' ' /proc/800/stat
  12. 1

(顶级父级PID将是在我到达init的PID之前的一个PID,即1.)

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

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

没有一个更好的解决方案,这是一个简单的(递归)脚本,以获得您给出的任何进程号的顶级父PID(或当前shell如果省略PID参数):
  1. #!/bin/bash
  2. # Look up the top-level parent Process ID (PID) of the given PID,or the current
  3. # process if unspecified.
  4.  
  5. function top_level_parent_pid {
  6. # Look up the parent of the given PID.
  7. pid=${1:-$$}
  8. stat=($(</proc/${pid}/stat))
  9. ppid=${stat[3]}
  10.  
  11. # /sbin/init always has a PID of 1,so if you reach that,the current PID is
  12. # the top-level parent. Otherwise,keep looking.
  13. if [[ ${ppid} -eq 1 ]] ; then
  14. echo ${pid}
  15. else
  16. top_level_parent_pid ${ppid}
  17. fi
  18. }

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

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

猜你在找的Bash相关文章