开发者

How do I stop Ctrl-C from killing spawned processes with jruby?

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.


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") }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜