我想阻止 CTRL-C 但它沒有按預期作業。我正在關注[here]( https://stackoverflow.com/a/37148777/12512199 )描述的答案,但沒有成功。
我一定錯過了什么,但不知道是什么。就好像 CTRL-C 被攔截但仍然傳播:
首先,我運行以下腳本并按 CTRL-C;顯示訊息,但腳本退出。
echo "
#!/bin/bash
trap 'echo "Ctrl C happened"' SIGINT
sleep infinity
" > test.sh
chmod x test.sh
./test.sh
然后我檢查了它在容器中的行為是否與 pid 1 不同:
echo "
#!/bin/bash
trap 'echo "Ctrl C happened"' SIGINT
sleep infinity
" > test.sh
chmod x test.sh
docker rm -f conttest
docker container create --name conttest -it --entrypoint="bash" ubuntu:20.04 -x -c /test.sh
docker cp test.sh conttest:/test.sh
docker container start --attach -i conttest
但不,這是相同的行為。我在 Unbuntu 20.04 上運行了這些測驗。閱讀https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap但仍然沒有找到任何線索......有什么想法嗎?
uj5u.com熱心網友回復:
Control C 或映射到intr輸出中的
任何其他組合鍵stty -a將 SIGINT 發送到前臺的所有行程。Shell 收到它,但它也收到了它,sleep infinity它死了,shell 退出了,因為它沒有任何關系。如果您希望腳本連續運行并在 SIGINT 上執行某些操作,則必須使用無限回圈:
#!/bin/bash
trap 'echo "Ctrl C happened"' SIGINT
while true
do
sleep infinity
done
如果您只想忽略 SIGINT:
#!/bin/bash
trap '' SIGINT
sleep infinity
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463871.html
下一篇:C結構型別令人困惑
