From: Charles Mills Date: 2004-10-10T14:28:42+09:00 Subject: Re: How to exit properly with a signal? On Oct 9, 2004, at 5:45 PM, Bob Proulx wrote: > How are signals properly handled in ruby? (I am new to ruby but > experienced with C, sh and perl.) > Here is a stab in the dark... how about this: $ ruby -e 'trap("INT") { |s| exit(s) }; sleep 10'; echo $? ^C2 $ or this: $ ruby -e 'trap("INT") { |s| exit(128 + s) }; sleep 10'; echo $? ^C130 $ Works like bash, seems to be what you want... ---- taken from bash man page: For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal N, bash uses the value of 128+N as the exit status. ----------------------------------------- -Charlie > Run, then interrupt it while it is in the sleep: > > sh -c 'sleep 10' ; echo $? > > 130 > > However, this same script in ruby is trapped by the ruby default > signal handler. > > ruby -e 'sleep 10' ; echo $? > -e:1:in `sleep': Interrupt from -e:1 > 1 > > How may I have ruby's program exit code properly reflect to the caller > that it was terminated on a signal? And also how may I prevent the > default handler from printing extraneous information? > > The perl version behaves as desired here similar to the shell version. > > perl -e 'sleep 10' ; echo $? > > 130