Stopping servers

When I run servers like WEBrick or an instance of TCPServer, it’s
unclear to me how to stop them, especially under Windows. Ctrl-C seems
to kill the server, but not the Ruby application in which it is
running. How can I detect that Ctrl-C has been pressed? Ctrl-Break
sends an INT signal that I can catch with trap(‘INT’). I’m not sure
what triggers trap(‘TERM’), but it’s not Ctrl-C.

Mark V. wrote:

When I run servers like WEBrick or an instance of TCPServer, it’s
unclear to me how to stop them, especially under Windows. Ctrl-C seems
to kill the server, but not the Ruby application in which it is
running. How can I detect that Ctrl-C has been pressed? Ctrl-Break
sends an INT signal that I can catch with trap(‘INT’). I’m not sure
what triggers trap(‘TERM’), but it’s not Ctrl-C.

My WEBrick code tends to have this line:

RUBY_PLATFORM =~ /mingw|mswin32/ ? signal = 1 : signal = “HUP”

and then, later on

s is the server instance

trap( signal ){ s.shutdown }

I also often map a ‘quit’ URL so I can stop the server through a
browser or wget:

s.mount_proc(‘/quit’) { |req, resp| s.shutdown; exit; }


James B.

http://www.ruby-doc.org - Ruby Help & Documentation
Ruby Code & Style - The Journal By & For Rubyists
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools