# Detecting control-c?

**URL:** <https://www.ruby-forum.com/t/detecting-control-c/96685>\
**Category:** Ruby\
**Created:** [December 8, 2005, 8:41pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685 "2005-12-08T20:41:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 8:41pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/1 "2005-12-08T20:41:01Z")

</div>

How does one detect Control-C?

I know that it raises an Interrupt, but there are other things that are  
also Interrupts (such as Timeout::Error). The documentation at  
[ruby-doc.org](http://ruby-doc.org) doesn’t list any details of class Interrupt, and the  
“Programming Ruby” book at [ruby-lang.org](http://ruby-lang.org) doesn’t even seem to mention  
that class Interrupt exists.

Thanks.

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 8:57pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/2 "2005-12-08T20:57:27Z")

</div>

William E. Rubin wrote:

> How does one detect Control-C?

trap(“INT”){ … }

> I know that it raises an Interrupt, but there are other things that are  
> also Interrupts (such as Timeout::Error). The documentation at  
> [ruby-doc.org](http://ruby-doc.org) doesn’t list any details of class Interrupt, and the  
> “Programming Ruby” book at [ruby-lang.org](http://ruby-lang.org) doesn’t even seem to mention  
> that class Interrupt exists.
> 
> Thanks.

Interrupt is a subclass of SignalException (which is a subclass of  
Exception).  
As far as I can tell (based on what I see in eval.c) it’s only used  
for  
trapping Unix signals\*, i.e. whatever’s in your signal.h file.

Also, take a look at the Signal module.

Regards,

Dan

- Works on Windows too, though the implementation is different, and  
likely  
requires a separate sleeper thread.

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 8, 2005, 9:05pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/3 "2005-12-08T21:05:34Z")

</div>

On 12/8/05, William E. Rubin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How does one detect Control-C?
> 
> I know that it raises an Interrupt, but there are other things that are  
> also Interrupts (such as Timeout::Error). The documentation at  
> [ruby-doc.org](http://ruby-doc.org) doesn’t list any details of class Interrupt, and the  
> “Programming Ruby” book at [ruby-lang.org](http://ruby-lang.org) doesn’t even seem to mention  
> that class Interrupt exists.

hump:[]:/home/mz652c% cat signal.rb

trap(“INT”) do  
puts “got signal INT”  
end

puts “Sup”  
gets

hump:[]:/home/mz652c% ruby signal.rb  
Sup  
got signal INT  
got signal INT  
got signal INT  
got signal INT  
got signal INT

pressing ctrl-c generates ‘got signal INT’. Had to press ctrl-d to  
exit the program.

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 9, 2005, 2:50pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/4 "2005-12-09T14:50:42Z")

</div>

On 12/8/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> got signal INT  
> Interesting. When I ran the program above I get:  
> ^Cgot signal INT  
> …

Any ideas on how to not get the ‘^C’ text?

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 9, 2005, 6:43pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/5 "2005-12-09T18:43:51Z")

</div>

On 12/9/05, Jim F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > got signal INT  
> > got signal INT
> 
> Interesting. When I ran the program above I get:  
> ^Cgot signal INT  
> …
> 
> Any ideas on how to not get the ‘^C’ text?

Dunno, I didn’t get the ‘^C’ text. (on linux)

---

<div class="post-metadata">

**Author:** ![williamerubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@williamerubin](https://www.ruby-forum.com/u/williamerubin)\
**Post date:** [December 9, 2005, 7:44pm UTC](https://www.ruby-forum.com/t/detecting-control-c/96685/6 "2005-12-09T19:44:09Z")

</div>

On Dec 9, 2005, at 8:50 AM, Jim F. wrote:

> > got signal INT  
> > got signal INT
> 
> Interesting. When I ran the program above I get:  
> ^Cgot signal INT  
> …
> 
> Any ideas on how to not get the ‘^C’ text?

The ^C is your ctrl-c being echoed by the  
terminal device driver. You can use the stty  
program to alter the behavior of the tty driver.  
In particular, take a look at the -echoctl  
option. In any case, the foreground process (ruby) doesn’t  
see the ctrl-c because the tty driver discards  
it and sends the Interrupt signal instead and then takes  
car of echoing ‘^C’ back to the terminal itself.

The specific behavior may also depend on the particular  
shell you are using.

Hope this helps.
