# Stopping servers

**URL:** <https://www.ruby-forum.com/t/stopping-servers/98660>\
**Category:** Ruby\
**Created:** [January 30, 2006, 4:37am UTC](https://www.ruby-forum.com/t/stopping-servers/98660 "2006-01-30T04:37:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 30, 2006, 4:37am UTC](https://www.ruby-forum.com/t/stopping-servers/98660/1 "2006-01-30T04:37:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 30, 2006, 5:29am UTC](https://www.ruby-forum.com/t/stopping-servers/98660/2 "2006-01-30T05:29:06Z")

</div>

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](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
