2

I'm developing a Sinatra application and I'm using "rackup" to start Webrick. What should I do to stop it? Now I'm using Ctrl+Z and it seems like it stops. However when I try to start it again it will say that the port is already bound.

I tried it with many ports and each time it started, stopped and then said it was in use when I restarted it again.

How do I solve it?

John Schmidt
  • 172
  • 9
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

2 Answers2

2

Ctrl+Z will just "pause" the process, not terminate / kill it.

To truly kill it, find it in the process table and do kill -9 [PID]

like:

ps auxwww | grep ruby
slivu  16244   0.0  0.5  2551140  61220 s020  R+    1:18AM   0:10.70 ruby app.rb

the second column(16244) is the PID.

The other way is to "catch" the INT signal with Ruby and exit the app explicitly.

in your app:

Signal.trap 'INT' do
    Process.kill 9, Process.pid
end
2

Extending on slivu's reply,

use CTRL+C to kill the process if you are still in the same terminal.

If you are launching it in the background, or want to kill from a different terminal, use

ps aux | grep [r]ackup | awk '{print $2}' | xargs sudo kill -9
  • @slivu that is just not true how does the `CTRL+C` work in rails with webrick server you missed that – Viren Oct 28 '12 at 14:57
  • Rails does the same trick i show here - trap the signal and exiting app. plain app backed by WEBRick can not be stopped by `CTRL-C` –  Oct 28 '12 at 15:03
  • 1
    "pkill -9 rackup" is shorter. – Catnapper Oct 29 '12 at 12:23