3

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.

4

1 に答える 1

0

親プロセスのみがによってSIGINT強制終了されます。子プロセスはSIGHUP、親プロセスが終了したことを示すシグナルが送信されているため、終了しています。コマンドで xeyes を起動してみてくださいnohupSIGHUPシグナルが起動するプロセスを強制終了するのを防ぎます。

Thread.new { system("nohup xeyes") }
于 2011-04-07T13:28:42.310 に答える