Question

I started learn Ruby. I tried to work with WEBrick server. After ruby index.rb command server starts. And how to stop it? I tried Ctrl+c, Ctrl+X and other. It still working. Then I pushed Ctrl+Z. Bash said: [1]+ Stopped ruby index.rb. But process is still working, and using my port, that need for server. I can kill it only from System Monitor.

What command I need to user in bash to kill index.rb process??

When I user Ctrl+C, there is error:

[2012-04-09 19:44:10] ERROR Interrupt: 
/usr/lib/ruby/1.8/webrick/server.rb:91:in `select'

P.S. Application is very simple (from WikiBooks):

   require 'webrick'
server = WEBrick::HTTPServer.new(:Port=>8080)
server.mount_proc('/'){ |req, resp|
    File.open('notepad.txt', 'w'){ |f| f.write(req.query["text"]) } if req.query["text"]
    resp['Content-Type'] = 'text/html'
    resp.body = %& <html><body><center><form method="post">
        <textarea name="text" rows="4" cols="40">#{IO.read('notepad.txt')}</textarea><br/>
        <input type="submit" name="update" value="save"/>
        </form></center></body></html></nowiki>& 
}
Was it helpful?

Solution

You can always kill your process by specifying a PID.

To get the pid from your server process you can use

$ ps a | grep ruby

Where PID is the first column, after that use: $ kill -9 <YOUR_PID> to kill the process.

Your server is not catching SIGINT, that's why you can't stop it with Ctrl+C.

Change your script to this

require 'webrick'

server = WEBrick::HTTPServer.new(:Port=>8080)

trap 'INT' do server.shutdown end

server.mount_proc('/'){ |req, resp|
    File.open('notepad.txt', 'w'){ |f| f.write(req.query["text"]) } if req.query["text"]
    resp['Content-Type'] = 'text/html'
    resp.body = %& <html><body><center><form method="post">
        <textarea name="text" rows="4" cols="40">#{IO.read('notepad.txt')}</textarea><br/>
        <input type="submit" name="update" value="save"/>
        </form></center></body></html></nowiki>& 
}

server.start

And enjoy Ctrl+C!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top