WEBrick catching keyboard interrupts

A

Alex Young

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...
 
M

MonkeeSage

Alex said:
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

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

Regards,
Jordan
 
A

Alex Young

MonkeeSage said:
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.
 
A

Alex Young

Alex said:
MonkeeSage said:
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.
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...
 
M

MonkeeSage

Alex said:
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
 
M

MonkeeSage

MonkeeSage said:
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
 
A

Alex Young

MonkeeSage said:
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

Oh, of course... Thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top