# WEBrick catching keyboard interrupts

**URL:** <https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102>\
**Category:** Ruby\
**Created:** [October 5, 2006, 10:48am UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102 "2006-10-05T10:48:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 10:48am UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/1 "2006-10-05T10:48:16Z")

</div>

Hi all,

I’m knocking up a few test scripts using WEBrick (on Ubuntu), and I’m  
constantly annoyed by the fact that hitting Ctrl-C doesn’t kill it, but  
makes it print the following to the console:

[2006-10-05 09:43:48] ERROR Interrupt:  
/usr/lib/ruby/1.8/webrick/server.rb:91:in `select’

I have to go to another console (or suspend it) and kill -9 the process.  
How can I stop this from happening? The code is very, very simple:

require ‘xmlrpc/server’

class Server \< XMLRPC::Server  
def initialize  
super(8080, ‘0.0.0.0’, 1)  
self.add\_handler(‘test.unit’){ 1 }  
self.serve  
end  
end

Server.new

I’m sure there’s a simple answer, but it eludes me…

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/2 "2006-10-05T13:16:32Z")

</div>

Alex Y. wrote:

> end  
> end
> 
> Server.new

Signal.trap(2) {  
“Server killed (sigint)”  
exit(1)  
}

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/3 "2006-10-05T13:40:35Z")

</div>

MonkeeSage wrote:

> > ```
> > self.serve
> > 
> > ```
> > 
> > end  
> > end
> > 
> > Server.new
> 
> Signal.trap(2) {  
> “Server killed (sigint)”  
> exit(1)  
> }

That gives me:

[2006-10-05 12:33:30] ERROR SystemExit: exit  
test\_server.rb:5:in `exit’

and a still-running process.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/4 "2006-10-05T13:42:36Z")

</div>

Alex Y. wrote:

> > > ```
> > > self.add_handler('test.unit'){ 1 }
> > > 
> > > ```
> 
> That gives me:
> 
> [2006-10-05 12:33:30] ERROR SystemExit: exit  
> test\_server.rb:5:in `exit’
> 
> and a still-running process.

Sorry, should have said: That gives me the above error on Ctrl-C, and  
repeats it for every time I send the interrupt, without ending the  
process. Just thought I’d clarify that…

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/5 "2006-10-05T14:06:38Z")

</div>

MonkeeSage wrote:

> I haven’t used webrick much but it looks like it uses an exit hook.  
> Apparently you use the #shutdown method:

Or better:

require ‘xmlrpc/server’  
class Server \< XMLRPC::Server  
def initialize  
Signal.trap(2) {  
puts “Server killed (sigint)”  
self.shutdown  
}  
super(8080, ‘0.0.0.0’, 1)  
self.add\_handler(‘test.unit’){ 1 }  
self.serve  
end  
end  
Server.new

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/6 "2006-10-05T14:18:40Z")

</div>

MonkeeSage wrote:

> ```
> puts "Server killed (sigint)"
> self.shutdown
> }
> super(8080, '0.0.0.0', 1)
> self.add_handler('test.unit'){ 1 }
> self.serve
> 
> ```
> 
> end  
> end  
> Server.new

Oh, of course… Thanks 🙂

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 5, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/webrick-catching-keyboard-interrupts/71102/7 "2006-10-05T14:02:38Z")

</div>

Alex Y. wrote:

> Sorry, should have said: That gives me the above error on Ctrl-C, and  
> repeats it for every time I send the interrupt, without ending the  
> process. Just thought I’d clarify that…

I haven’t used webrick much but it looks like it uses an exit hook.  
Apparently you use the #shutdown method:

require ‘xmlrpc/server’  
class Server \< XMLRPC::Server  
def initialize  
super(8080, ‘0.0.0.0’, 1)  
self.add\_handler(‘test.unit’){ 1 }  
self  
end  
def start  
self.serve  
end  
end  
server = Server.new  
Signal.trap(2) {  
puts “Server killed (sigint)”  
server.shutdown  
}  
server.start

Regards,  
Jordan
