如何阻止 Ctrl-C 使用 jruby 杀死生成的进程?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只有父进程被
SIGINT
杀死,子进程正在死亡,因为它们收到了SIGHUP
信号,表明它们的父进程已经死亡。尝试通过 nohup 命令启动 xeyes。它将防止 SIGHUP 信号杀死它启动的进程。Only the parent process is killed by
SIGINT
, the child processes are dying because they're being sent aSIGHUP
signal that indicates their parent process has died. Try launching xeyes via thenohup
command. It will prevent theSIGHUP
signal from killing the processes it launches.