使用 shell 脚本捕获 SIGINT 的问题
Problem with trapping SIGINT using shell script
我想阻止 CTRL-C,但它没有按预期工作。
我正在按照 [此处] () 描述的答案进行操作,但没有成功。
我一定是漏掉了什么,但又想不通。好像 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 但仍未找到任何线索...
有什么想法吗?
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
我想阻止 CTRL-C,但它没有按预期工作。
我正在按照 [此处] (
我一定是漏掉了什么,但又想不通。好像 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 但仍未找到任何线索... 有什么想法吗?
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