WEBrick catching keyboard interrupts

Discussion in 'Ruby' started by Alex Young, Oct 5, 2006.

  1. Alex Young

    Alex Young Guest

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

    --
    Alex
     
    Alex Young, Oct 5, 2006
    #1
    1. Advertising

  2. Alex Young

    MonkeeSage Guest

    Alex Young wrote:
    > 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
     
    MonkeeSage, Oct 5, 2006
    #2
    1. Advertising

  3. Alex Young

    Alex Young Guest

    MonkeeSage wrote:
    > Alex Young wrote:
    >> 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)
    > }
    >

    That gives me:

    [2006-10-05 12:33:30] ERROR SystemExit: exit
    test_server.rb:5:in `exit'

    and a still-running process.

    --
    Alex
     
    Alex Young, Oct 5, 2006
    #3
  4. Alex Young

    Alex Young Guest

    Alex Young wrote:
    > MonkeeSage wrote:
    >> Alex Young wrote:
    >>> 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)
    >> }
    >>

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

    --
    Alex
     
    Alex Young, Oct 5, 2006
    #4
  5. Alex Young

    MonkeeSage Guest

    Alex Young 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
     
    MonkeeSage, Oct 5, 2006
    #5
  6. Alex Young

    MonkeeSage Guest

    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
     
    MonkeeSage, Oct 5, 2006
    #6
  7. Alex Young

    Alex Young Guest

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


    Oh, of course... Thanks :)

    --
    Alex
     
    Alex Young, Oct 5, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D.Sn

    Interrupts in VB (API)

    D.Sn, Jan 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,783
    Willy Denoyette [MVP]
    Jan 12, 2004
  2. Victor Bazarov

    Keyboard interrupts

    Victor Bazarov, Mar 28, 2005, in forum: C++
    Replies:
    8
    Views:
    8,818
    Ioannis Vranos
    Mar 29, 2005
  3. John Ladasky

    Keyboard interrupts, Idle vs. SciTE

    John Ladasky, Nov 2, 2006, in forum: Python
    Replies:
    4
    Views:
    480
    John Ladasky
    Nov 3, 2006
  4. Lloyd Zusman
    Replies:
    2
    Views:
    433
  5. Aaron Smith

    WEBrick::Daemon and WEBrick

    Aaron Smith, Mar 9, 2007, in forum: Ruby
    Replies:
    2
    Views:
    341
    Aaron Smith
    Mar 18, 2007
Loading...

Share This Page