Hi!
I'm having trouble trapping Ctrl-C. I have a CLI that spawns processes I would like to survive the CLI, but if I press Ctrl-C the spawns dies along with the CLI. I have experimented with trap(INT), and have boiled down the problem into a small example program. I run this on Linux.
First, the program:
-------- 8< --------
#!/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("xeyes") }
else
keep_at_it = false
end
end
-------- 8< --------
Now, the behaviour:
When using jruby (1.6.0):
* Quitting leaves spawned processes running
* Ctrl-C kills all spawned processes, writes "Ctrl-C!" and quits after next return. I have used strace to verify that the xeyes actually gets a SIGINT, not a SIGHUP.
When using ruby (1.8.7):
* Quitting leaves spawned processes running
* First time Ctrl-C is pressed, the first started xeyes dies.
* Second time Ctrl-C is pressed "Ctrl-C!" is written and the program exists after next return, leaving remaining spawned processes alive.
I don't understand the behaviour. The jruby version feels more consistent, but is far away from what I need. The ruby version feels strange, but I could handle my needs by spawning a dummy process if I didn't need jmx4r (which as far as I understand requires jruby).
1. How is trapping of SIGINT supposed to work? How is the passing of SIGINT supposed to work?
2. Why does it differs between ruby and jruby?
3. Anyone with a suggestion how to spawn processes not affected by a SIGINT?
///BR, Jens Carlberg