我们有一台机器使用 tail -f file,查看日志时,使用 ctrl+c 无法中断退出。导致每次使用 ctrl+z 挂起再杀掉,十分麻烦。
正常情况下使用 ctrl+c 会终止前台运行的程序,因为 ctrl+c 会向进程发送一个终止信号。联想到 trap 是不是设置有问题。
问题主机:
[root@zabbix_a5 ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.2.1511 (Core)
Release: 7.2.1511
Codename: Core
[root@zabbix_a5 ~]# trap
trap -- '' SIGINT
trap -- '' SIGQUIT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
正常主机:
[root@VM_85_116_centos test]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
[root@VM_85_116_centos test]# trap
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
问题主机有下面的设置,导致 SIGINT ( ctrl+c )、SIGQUIT 信号会被忽略。
trap -- '' SIGINT
trap -- '' SIGQUIT
[root@zabbix_a5 ~]# trap
trap -- '' SIGINT
trap -- '' SIGQUIT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
[root@zabbix_a5 ~]# trap - SIGINT
[root@zabbix_a5 ~]# trap
trap -- '' SIGINT
trap -- '' SIGQUIT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
[root@VM_85_116_centos ~]# trap -- '' SIGINT
[root@VM_85_116_centos ~]# trap
trap -- '' SIGINT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
[root@VM_85_116_centos ~]# trap - SIGINT
[root@VM_85_116_centos ~]# trap
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
问题主机 trap - SIGINT
无法清楚 trap 设置
编写脚本 test.sh
echo '111'
trap 'ls' SIGINT
echo '222'
trap 'ls' SIGINT
echo '333'
trap - SIGINT
分别在正常与问题主机上执行
strace -o output.log -v sh test.sh
问题主机摘要:
write(1, "111\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(1, "222\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(1, "333\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigaction(SIGINT, {0x456a90, [], SA_RESTORER, 0x7f76eedc7250}, {SIG_IGN, [], SA_RESTORER, 0x7f76eedc7250}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
read(255, "", 81) = 0
exit_group(0) = ?
+++ exited with 0 +++
正常主机摘要:
write(1, "111\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
rt_sigaction(SIGINT, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, {SIG_DFL, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(1, "222\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
rt_sigaction(SIGINT, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(1, "333\n", 4) = 4
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7fed821c4280}, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigaction(SIGINT, {0x456be0, [], SA_RESTORER, 0x7fed821c4280}, {SIG_DFL, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
read(255, "", 81) = 0
exit_group(0) = ?
+++ exited with 0 +++
执行trap 'ls' SIGINT
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
rt_sigaction(SIGINT, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
执行trap - SIGINT
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigaction(SIGINT, {0x456a90, [], SA_RESTORER, 0x7f76eedc7250}, {SIG_IGN, [], SA_RESTORER, 0x7f76eedc7250}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7fed821c4280}, {0x454380, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigaction(SIGINT, {0x456be0, [], SA_RESTORER, 0x7fed821c4280}, {SIG_DFL, [], SA_RESTORER, 0x7fed821c4280}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
貌似是问题主机少了一次 rt_sigaction 系统调用
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.