如何在非交互式 bash 脚本及其 git 子程序中禁用 ctrl-c

How to disable ctrl-c in a non-interactive bash script and its git subprograms

我正在从非交互式 bash shell 调用 git 克隆。它是非交互式的,因为它是从 Windows Explorer contect 菜单启动的。我是 运行 git 版本 2.20.1.windows.1 Windows 10 64 位。

我是 运行 Git/usr/bin/bash -l -e 'myscript' 在 Git/usr/bin/mintty window 从上下文菜单中启动。

我想阻止用户使用 ctrl-c 中断 git 克隆。

我试过了:

set -m
trap '' SIGINT SIGTERM (2 single quotes)
git clone ... &
wait -n
echo $?

ctrl-c 传递给退出的git克隆。我假设它有一个在 SIGINT 上退出的信号处理程序。我想知道为什么这不起作用。

我试过了:

saved=$(stty -g)
stty -isig
git clone ...
stty "$saved"

stty 失败并显示 "stty: standard input: Inappropriate ioctl for device",因为没有用于非交互式 bash shell 的 tty。那么,如果没有 tty,ctrl-c 如何到达 git 克隆?

我正在使用 git 克隆进行测试,但想为 git 拉取和 git 推送部署它。我们的开发人员通过使用 ctrl-c 中断长 git 拉取而导致他们的本地存储库不一致。

如有任何帮助,我们将不胜感激。

自己处理信号的进程(示例似乎是 gitpingscp)阻止父 trap 被调用。

所以这个简单的例子不适合这些目的:

#!/bin/bash
trap '' SIGINT SIGTERM
sleep 10

这个answer建议在不向进程发送SIGINT的子shell中使用set -m(进程运行在单独的进程组中):

#!/bin/bash
trap '' SIGINT SIGTERM
uninterruptableCommand="git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/"
( set -m; $uninterruptableCommand & wait )

警告:该示例是一个非常长的 运行ning 命令。您仍然可以直接向进程发送信号(例如 pkill git)。