如何阻止 Ctrl-C 使用 jruby 杀死生成的进程?

发布于 2024-10-31 02:37:17 字数 986 浏览 4 评论 0原文

我有一个 ruby​​ 程序,生成新进程。即使我按下 Ctrl-C,我也希望它们能够在其父级中幸存下来。为了实现这一点,我尝试捕获 INT,但是,这没有帮助。

下面的程序在您每次按 Enter 时启动 xeyes,如果您写入任何内容则退出,并且如果您按 Ctrl-C 然后返回则应该退出。

  • 如果我放弃正常的方式,xeyes 就会存活下来。
  • 如果我按 Ctrl-C,xeyes 就会消失。
  • 跟踪 xeyes,它确实收到了 SIGINT,而不是建议的 SIGHUP。

我该怎么做才能让我的眼睛保持活力?

程序:

#!/usr/bin/jruby
require 'readline'

keep_at_it = true

trap("INT") { puts "\nCtrl-C!" ; keep_at_it = false }

while (keep_at_it) do
  line = Readline.readline("Enter for new xeyes, anything else to quit: ", true)
  if (line.length == 0 && keep_at_it == true)
    Thread.new { system("nohup xeyes >/dev/null 2>/dev/null") }
  else
    keep_at_it = false
  end
end

我也一直在使用 ruby​​ 进行测试,但是由于我需要仅适用于 jruby 的 JMX 支持,因此我无法按原样使用 ruby​​。以下方式在 ruby​​ 中有效:

fork { Process.setsid; exec("xeyes") }

“Processsetsid”似乎确保没有控制终端,我怀疑这是核心。但是,即使使用 -J-Djruby.fork.enabled=true 标志,我也无法让 jruby 接受 fork。

I have a ruby program, spawning new processes. I want these to survive their parent even when I press Ctrl-C. To accomplish this, I try to trap INT, However, this doesn't help.

The program below starts an xeyes each time you press enter, quits if you write anything, and is supposed to quit if you press Ctrl-C and then return.

  • If I quit the normal way, the xeyes survives.
  • If I press Ctrl-C, the xeyes dies.
  • Tracing the xeyes, it do receive a SIGINT, not a SIGHUP as suggested.

What can I do to keep my xeyes alive?

The program:

#!/usr/bin/jruby
require 'readline'

keep_at_it = true

trap("INT") { puts "\nCtrl-C!" ; keep_at_it = false }

while (keep_at_it) do
  line = Readline.readline("Enter for new xeyes, anything else to quit: ", true)
  if (line.length == 0 && keep_at_it == true)
    Thread.new { system("nohup xeyes >/dev/null 2>/dev/null") }
  else
    keep_at_it = false
  end
end

I have been testing with ruby as well, but since I need JMX support thats only available with jruby, I cannot use ruby as-is. The following way works in ruby:

fork { Process.setsid; exec("xeyes") }

The 'Process setsid' seems to make sure there is no controlling terminal, and I suspect this is central. However, I fail in getting jruby to accept fork, even using the -J-Djruby.fork.enabled=true flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-11-07 02:37:17

只有父进程被 SIGINT 杀死,子进程正在死亡,因为它们收到了 SIGHUP 信号,表明它们的父进程已经死亡。尝试通过 nohup 命令启动 xeyes。它将防止 SIGHUP 信号杀死它启动的进程。

Thread.new { system("nohup xeyes") }

Only the parent process is killed by SIGINT, the child processes are dying because they're being sent a SIGHUP signal that indicates their parent process has died. Try launching xeyes via the nohup command. It will prevent the SIGHUP signal from killing the processes it launches.

Thread.new { system("nohup xeyes") }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文