【问题标题】:How to disable control-c in ruby如何在 ruby​​ 中禁用 control-c
【发布时间】:2019-07-28 13:38:25
【问题描述】:

我正在开发一个程序,该程序需要您输入密码才能退出 ruby​​ 程序,我需要一种方法来禁用 control-c,因为它使我的邪恶计划失败。我需要做到这一点,这样用户就不能中断程序,而没有一堆这些:

rescue Interrupt
    while true
        puts "\nExiting..."
    end
end

【问题讨论】:

  • 一堆?你只需要这个。
  • 我怀疑有人会搜索带有“macos”标签的问题。
  • 用户可以从终端发送quite a few more 信号,例如SIGQUITCTRL+\ SIGTSTPCTRL+Z。根据您使用的外壳,可能还有更多。出于任何实际的安全目的,您可能不应该强烈依赖能够捕获所有这些信号。

标签: ruby macos


【解决方案1】:

你可以像这样捕获 SIGINT

trap "SIGINT" do
  # this is called when you press control-c
  # be very careful, you can't kill this program with control-c
end

你也可以看Capturing Ctrl-c in ruby 其他一些与control-c交互的方式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多