简体   繁体   English

使用陷阱USR1时Bash随机函数的行为

[英]Bash random function behavior when using trap USR1

I have a bash code that gives me random results. 我有一个bash代码,可以给我随机结果。 I have stripped down my code to some pseudo code that looks like: 我已经将代码简化为一些伪代码,如下所示:

#!/usr/bin/bash

function TrapQuit {
    echo "Quitting"
}

function SubFunction {
    echo "Full function call tree ${FUNCNAME[@]}"
}

function DisPatch {
    echo "Running function ${FUNCNAME[0]}"
    SubFunction "1"
}

function test {
    kill -USR1 $$

}

trap DisPatch USR1
trap TrapQuit EXIT HUP

test &
test &
test &
test &
test &

while true; do
    sleep 1
done

Basically, ${FUNCNAME[@]} should list all function calls from current function up to main. 基本上,$ {FUNCNAME [@]}应该列出从当前函数到main的所有函数调用。 When I run this code, I get randomly different results. 当我运行此代码时,我会随机得到不同的结果。

The good result is: 好的结果是:

Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch test main ^CRunning function DisPatch Full function call tree SubFunction DisPatch main Quitting

The strange result is: 奇怪的结果是:

Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch SubFunction DisPatch DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main ^CQuitting

Once every three or five runs, I get the weird result where DisPatch function is three times in ${FUNCNAME[@]}. 每运行三到五次,就会得到一个奇怪的结果,其中DisPatch函数是$ {FUNCNAME [@]}中的三倍。

How can the DisPatch function end up multiple times in ${FUNCNAME[@]} from SubFunction where it seems that DisPatch has executed itself recursively ? DisPatch函数如何才能在SubFunction的$ {FUNCNAME [@]}中多次出现,而SubFunction似乎已经递归执行了DisPatch?

Thanks for any insight. 感谢您的见解。

Tested with 经过测试

GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu) under CentOS 7 CentOS 7下的GNU bash版本4.2.46(1)-发行版(x86_64-redhat-linux-gnu)

GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu) under Fedora 24 Fedora 24下的GNU bash版本4.3.42(1)-发行版(x86_64-redhat-linux-gnu)

There are two problems. 有两个问题。 First, $$ is always the process ID of the top-most shell; 首先, $$始终是最顶层shell的进程ID; subshells do not reset its value. 子壳不会重置其值。 Use kill -USR1 $BASHPID instead. 请使用kill -USR1 $BASHPID Second, subprocesses do not inherit traps. 其次,子流程不继承陷阱。 You need to move trap DisPath USR1 into the definition of test . 您需要将trap DisPath USR1移到test的定义中。

In the end, your test should look like 最后,您的test应该看起来像

function test {
    trap DisPatch USR1
    kill -USR1 $BASHPID
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM