Bash 查找进程的所有祖先和子代
我需要编写一个递归函数,它将接受一个PID并找到它的父级、祖先级和子级。这是我写的,但似乎不起作用 此函数仅适用于用户输入的进程的祖先Bash 查找进程的所有祖先和子代,bash,unix,recursion,Bash,Unix,Recursion,我需要编写一个递归函数,它将接受一个PID并找到它的父级、祖先级和子级。这是我写的,但似乎不起作用 此函数仅适用于用户输入的进程的祖先 findFather(){ if [ $1 -ne 1 ]; then pid=$1 ppid=ps -ef | awk '$pid == $2' findFather $ppid echo $ppid fi findFather $1 首先,使用$(…)捕获awk管道的输出。其次,使用awk-v将变量传递到awk脚本中 ppid=$
findFather(){
if [ $1 -ne 1 ]; then
pid=$1
ppid=ps -ef | awk '$pid == $2'
findFather $ppid
echo $ppid
fi
findFather $1
首先,使用$(…)
捕获awk管道的输出。其次,使用awk-v
将变量传递到awk脚本中
ppid=$(ps -ef | awk -v pid="$pid" 'pid == $2')
或者,使用双引号,但确保转义$2
ppid=$(ps -ef | awk "$pid == \$2")
例如,您缺少awk语句末尾的“'”
接下来,由于缺少定义函数结束位置的“}”,您还没有关闭函数
我不确定是否可以从函数本身调用“findFather”函数
我建议您运行“ps-ef”命令,然后使用awk或grep获取包含PID的所有行-可能将它们存储在一个数组中,然后您可以awk该数组并为这些行获取PID字段。。。这将为您提供子进程PID
希望这是有道理的,也许有更好的办法 这就是我的目的:
findFather(){
ppid=$(ps ho ppid $1)
echo $ppid
[ $ppid -gt 1 ] && findFather $ppid
}
对于孩子们:
findChilds() {
for child in $(ps --ppid $1 ho pid) ;do
echo $child
findChilds $child
done
}
与分层演示相同:
findChilds() {
for child in $(ps --ppid $1 ho pid);do
[ $child -eq $$ ] && echo $child Current ||
echo $child ":(" $(findChilds $child) ")"
done
}
下一步:执行正确的缩进。
pstree
来自psmisc
包:
*$ sleep 60 & sleep 60 & sleep 60 &
[1] 2196
[2] 2197
[3] 2198
$ echo $$
1876
$ pstree -sp $$
systemd(1)───gnome-terminal-(3381)───fish(1186)───bash(1876)─┬─pstree(2224)
├─sleep(2196)
├─sleep(2197)
└─sleep(2198)*
结果类似:
bash,2342
`-python,23264 /usr/local/bin/<some command> ...
`-ssh,25982 -t -x -oStrictHostKeyChecking=no -oPubkeyAuthentication=n
bash,2342
`-python,23264/usr/local/bin/。。。
`-ssh,25982-t-x-oStrictHostKeyChecking=no-oPubkeyAuthentication=n
您只需执行pstree-p
。它应该打印出带有程序名的pid
。第一种方式:
父项的所有子项(=bash):
第二种方式:
所有子项递归:
~ ❱ sleep 10 & sleep 10 & sleep 10 & sleep 10 &
[2] 6077
[3] 6078
[4] 6079
[5] 6080
~ ❱ ps -o time,pid,ppid,cmd --forest -g -p $(pgrep -x bash)
TIME PID PPID CMD
00:00:01 2986 2984 /bin/bash
00:00:00 4731 2986 \_ redshift
00:00:00 6077 2986 \_ sleep 10
00:00:00 6078 2986 \_ sleep 10
00:00:00 6079 2986 \_ sleep 10
00:00:00 6080 2986 \_ sleep 10
00:00:00 6082 2986 \_ ps -o time,pid,ppid,cmd --forest -g -p 2986
~ ❱
在.bashrc
function subps()
{
process=$(pgrep -x $1)
ps -o time,pid,ppid,cmd --forest -g -p $process
}
然后在命令行上:
~ ❱ sleep 10 & sleep 10 & sleep 10 & sleep 10 &
[1] 6141
[2] 6142
[3] 6143
[4] 6144
~ ❱ subps bash
TIME PID PPID CMD
00:00:00 6126 6124 /bin/bash
00:00:00 6141 6126 \_ sleep 10
00:00:00 6142 6126 \_ sleep 10
00:00:00 6143 6126 \_ sleep 10
00:00:00 6144 6126 \_ sleep 10
00:00:00 6146 6126 \_ ps -o time,pid,ppid,cmd --forest -g -p 6126
~ ❱
第三条道路:
使用htop
命令;然后使用F3
进行搜索,使用F5
进行树状列表:
注意
F5
有两种模式:树
和排序
第一个pid是1而不是0!这是不是在2美元后丢失的单报价?例如,不应该是“$pid==$2”吗?@F.Hauri仍然有效。为什么不使用ps ho ppid$pid
而不是使用awk进行过滤?您需要自己的功能吗?例如,您可以使用pstree来获取parents和childrenJohn,ppid
的命令替代是否打算从ps
返回整行?e、 g.ppid
=david 1521 1483 0 Apr24 pts/2 00:00:01/bin/bash
?似乎您只需要下一个pid
?
function subps()
{
process=$(pgrep -x $1)
ps -o time,pid,ppid,cmd --forest -g -p $process
}
~ ❱ sleep 10 & sleep 10 & sleep 10 & sleep 10 &
[1] 6141
[2] 6142
[3] 6143
[4] 6144
~ ❱ subps bash
TIME PID PPID CMD
00:00:00 6126 6124 /bin/bash
00:00:00 6141 6126 \_ sleep 10
00:00:00 6142 6126 \_ sleep 10
00:00:00 6143 6126 \_ sleep 10
00:00:00 6144 6126 \_ sleep 10
00:00:00 6146 6126 \_ ps -o time,pid,ppid,cmd --forest -g -p 6126
~ ❱