V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
notolddriver
V2EX  ›  Linux

Centos trap 设置无法删除

  •  
  •   notolddriver · 2019-07-19 14:13:49 +08:00 · 2648 次点击
    这是一个创建于 1735 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题

    我们有一台机器使用 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 设置

    strace 分析

    编写脚本 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 系统调用

    问下哪位大佬可以解释一下吗?或者还有别的方法删除 trap 设置吗?

    第 1 条附言  ·  2019-07-20 15:35:29 +08:00

    将下面的重置命令追加到系统的 /etc/profile 后面,然后再次进入终端,重置成功了。 在终端直接重置为什么不成功,还不清楚。

    trap SIGINT
    trap SIGQUIT
    
    6 条回复    2019-07-25 03:17:08 +08:00
    ps1aniuge
        1
    ps1aniuge  
       2019-07-20 00:24:14 +08:00
    楼主你好,我认为你很牛 x,谢谢分享帖子。
    yum update kernel。若不行,重装 centos7.6。
    stern123
        2
    stern123  
       2019-07-20 09:14:30 +08:00 via iPhone
    1,这个问题是突然出现的还是一直就有?
    2,在终端下和 X 下都有问题吗?还是仅 ssh 过去出问题?
    stern123
        3
    stern123  
       2019-07-20 09:18:38 +08:00 via iPhone
    我之前遇到的一个问题是:一个热键在终端下能用,但在 Gnome 下却不响应。dmesg 看能发现 gnome 还是会改系统设置的。
    notolddriver
        4
    notolddriver  
    OP
       2019-07-20 15:36:13 +08:00
    @ps1aniuge 这个是公司的一台比较重要的机器 不可以重装哦。不过现在问题暂时解决了。
    notolddriver
        5
    notolddriver  
    OP
       2019-07-20 15:38:04 +08:00
    @stern123
    1. 听同事说是另一个同事装了 ruby 相关的什么东西后,出现的。
    2. 公司的服务器,没有 X 环境。ssh 终端上问题。 现在已经暂时解决了。
    aliipay
        6
    aliipay  
       2019-07-25 03:17:08 +08:00
    lz 要不 gdb bash 试下? trap 对应函数是 trap_builtin
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1290 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:32 · PVG 07:32 · LAX 16:32 · JFK 19:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.