[英]How to disable ctrl-c in a non-interactive bash script and its git subprograms
我正在從非交互式 bash shell 調用 git clone。 它是非交互式的,因為它是從 Windows 資源管理器的連接菜單中啟動的。 我在 Windows 10 64 位上運行 git 版本 2.20.1.windows.1。
我正在從上下文菜單啟動的 Git/usr/bin/mintty 窗口中運行 Git/usr/bin/bash -l -e 'myscript' 。
我想阻止用戶使用 ctrl-c 中斷 git clone。
我試過:
set -m
trap '' SIGINT SIGTERM (2 single quotes)
git clone ... &
wait -n
echo $?
ctrl-c 傳遞到退出的 git clone。 我假設它有一個在 SIGINT 上退出的信號處理程序。 我想知道為什么這不起作用。
我試過:
saved=$(stty -g)
stty -isig
git clone ...
stty "$saved"
stty 因“stty:標准輸入:設備的 ioctl 不合適”而失敗,因為沒有用於非交互式 bash shell 的 tty。 那么如果沒有 tty,ctrl-c 是如何到達 git clone 的呢?
我正在使用 git clone 進行測試,但想為 git pull 和 git push 部署它。 我們的開發人員通過使用 ctrl-c 中斷長時間的 git pull 導致他們的本地存儲庫不一致。
任何幫助,將不勝感激。
自己處理信號的進程(例如git
、 ping
和scp
)會阻止調用父trap
。
所以這個簡單的例子不適合這些目的:
#!/bin/bash
trap '' SIGINT SIGTERM
sleep 10
這個答案建議在不向進程發送 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 )
警告:該示例是一個真正長時間運行的命令。 您仍然可以直接向進程發送信號(例如pkill git
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.