linux - Bash 陷阱不会杀死 child ,导致意外的 ctrl-c 行为

标签 linux bash inotify

编辑

对于 future 的读者。这个问题的根源实际上归结为在交互式 shell 中运行函数而不是将其放在单独的脚本中。

此外,我最初发布的代码中还有很多地方可以改进。请查看有关可以/应该做得更好的事情的评论。

/编辑

我有一个 bash 函数,用于在目录中的文件更改时在后台重新运行进程(类似于 Grunt,但用于一般用途)。脚本在运行时按预期运行:

  • 子进程正确启动(包括任何子进程)
  • 在文件更改时,子程序被杀死(包括子程序)并重新启动

但是,在退出时 (ctrl-c) 没有进程被终止。此外,再次按 ctrl-c 将终止当前终端 session 。我假设这是我的陷阱的问题,但一直无法确定问题的原因。

这里是rerun.sh的代码

#!/bin/bash
# rerun.sh

_kill_children() {
    isTop=$1
    curPid=$2
        # Get pids of children
    children=`ps -o pid --no-headers --ppid ${curPid}`
    for child in $children
    do
            # Call this function to get grandchildren as well
            _kill_children 0 $child
    done
    # Parent calls this with 1, all other with 0 so only children are killed
    if [[ $isTop -eq 0 ]]; then
            kill -9 $curPid 2> /dev/null
    fi
}

rerun() {
    trap " _kill_children 1 $$; exit 0" SIGINT SIGTERM
    FORMAT=$(echo -e "\033[1;33m%w%f\033[0m written")
    #Command that should be repeatedly run is passed as args
    args=$@
    $args &

    #When a file changes in the directory, rerun the process
    while inotifywait -qre close_write --format "$FORMAT" .
    do
        #Kill current bg proc and it's children
        _kill_children 1 $$
        $args & #Rerun the proc
    done
}

#This is sourced in my bash profile so I can run it any time

要对此进行测试,请创建一对可执行文件 parent.sh 和 child.sh,如下所示:

#!/bin/bash
#parent.sh
./child.sh

#!/bin/bash
#child.sh
sleep 86400

然后获取 rerun.sh 文件并运行 rerun ./parent.sh。在另一个终端窗口中,我 watch "ps -ef | grep pts/4" 以查看重新运行的所有进程(在此示例中为 pts/4)。触摸目录中的文件会触发 parent.sh 和子项的重新启动。 [ctrl-c] 退出,但让 pids 继续运行。 [ctrl-c] 再次杀死 bash 和 pts/4 上的所有其他进程。

期望的行为:在 [ctrl-c] 上,杀死 child 并正常退出到 shell。帮忙?

-- 代码来源:

Inotify 想法来自:https://exyr.org/2011/inotify-run/

杀死 child 来自:http://riccomini.name/posts/linux/2012-09-25-kill-subprocesses-linux-bash/

最佳答案

首先,这不是一个值得遵循的好做法。明确跟踪您的 child :

children=( )
foo & children+=( "$!" )

...然后,您可以显式地杀死或等待它们,引用 "${children[@]}" 获取列表。如果你也想要孙子,这是 fuser -k 和锁文件的好用户:

lockfile_name="$(mktemp /tmp/lockfile.XXXXXX)" # change appropriately
trap 'rm -f "$lockfile_name"' 0

exec 3>"$lockfile_name" # open lockfile on FD 3
kill_children() {
    # close our own handle on the lockfile
    exec 3>&-

    # kill everything that still has it open (our children and their children)
    fuser -k "$lockfile_name" >/dev/null

    # ...then open it again.
    exec 3>"$lockfile_name"
}

rerun() {
    trap 'kill_children; exit 0' SIGINT SIGTERM
    printf -v format '%b' "\033[1;33m%w%f\033[0m written"

    "$@" &

    #When a file changes in the directory, rerun the process
    while inotifywait -qre close_write --format "$format" .; do
        kill_children
        "$@" &
    done
}

关于linux - Bash 陷阱不会杀死 child ,导致意外的 ctrl-c 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521078/

相关文章:

linux - 我们如何为同一台 Linux 机器上的所有用户安装 rustc、cargo?

linux - 为什么在上下文切换期间 TLB(Translation Look Side buffer)存储在 PCB 中?

java - 我正在尝试通过java编程将Sqoop从oracle导入到hdfs

php - 如何在 Debian Linux 中允许用户 www-data 访问 i2c?

linux - 如何列出 Linux 组中的所有用户?

bash - Vim - 在搜索时捕获字符串并在替换时使用

linux - 如何将命令后面的文本分配给 bash 中的变量

c - 如何打印 C 目录中新创建的文件的名称?

linux - 添加目录中的文件后,如何使用 inotify-tools 向我发送电子邮件?

ruby-inotify 例子?