From: Charles Oliver Nutter Date: 2011-04-11T08:32:00+09:00 Subject: Re: How do I trap Ctrl-C so it doesn't reach spawned threads? On Fri, Apr 8, 2011 at 2:53 AM, Jens Carlberg wrote: > 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. ... > 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? In MRI, I *believe* SIGINT is sent to the process that currently controls the TTY, so in the case of system() it's sent to your program that's running. In JRuby, child processes don't usually share the TTY streams (JVM isolates child process streams from the parent using pipes), so Ctrl-C is sent to JRuby (the JVM itself, really, since we don't trap INT in JRuby and allow JVM to handle it) and it shuts down taking the active child process with it. Perhaps you can try to use posix_spawn or 1.9's "spawn"? - Charlie