8

I'm trying to run the following Sinatra application and am getting an error message telling me that I can't start a server, either because port's already in use or because I don't have root privileges. I have never had this problem before starting a Sinatra application. I updated to Mountain Lion for my mac a few days ago and wonder if this might be the cause of the problem. I also use RVM. Can anyone provide a suggestion...

require "sinatra"

class MyApp < Sinatra::Base
 get '/' do
    "Hello from MyApp"

 end

end 

== Sinatra/1.3.3 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/eventmachine-1.0.0/lib/eventmachine.rb:526:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)

Update: I can still run a rails server on my machine so I think the problem is specific to Sinatra. Furthermore, I was able to run Sinatra applications fine up til a few days ago, when I started playing around with this Rack Tutorialwhich instructed me to explicitly to set a port. I'm wondering if that made a permanent change.

>> Rack::Handler::WEBrick.run my_rack_proc, :Port => 9876
[2011-10-24 11:32:21] INFO  WEBrick 1.3.1
[2011-10-24 11:32:21] INFO  ruby 1.9.2 (2011-07-09) [i386-mingw32]
[2011-10-24 11:32:21] INFO  WEBrick::HTTPServer#start: pid=480 port=9876
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134
  • run "netstat" to see if the port is in use. Otherwise, it's permissions :) – paulsm4 Dec 27 '12 at 05:30
  • I've never used netstat before. Is it a command I run in the terminal. A quick google search shows there's a 'netstat' in the network utility application. would I be asking it to display "state of current socket connections"? – BrainLikeADullPencil Dec 27 '12 at 05:35
  • Try it. It won't hurt anything, and you'll learn how it works. – the Tin Man Dec 27 '12 at 05:46
  • Yes, it's a command (`netstat -a`) you'd run in a terminal :) Here's the "man" page (another shining artifact of command-line days: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/netstat.1.html – paulsm4 Dec 27 '12 at 05:47
  • would this indicate that a local port's running? tcp4 0 0 *.57600 *.* LISTEN – BrainLikeADullPencil Dec 27 '12 at 05:49
  • Yes, that would indicate some process has "bound" to port 57600. You're interested in seeing if anybody is using port 4567. – paulsm4 Dec 27 '12 at 05:52
  • @paulsm4 nobody is using port 4567. Permissions then? but I don't understand why. I've never had this problem before, been learning ruby for 1 year. – BrainLikeADullPencil Dec 27 '12 at 05:55
  • 3
    `lsof -i TCP | grep LISTEN` will show apps listening on a particular TCP port. – the Tin Man Dec 27 '12 at 06:06
  • `lsof -i :4567` will show the particular port info too. – ian Dec 30 '12 at 14:54

2 Answers2

15

Note: I had a different answer before. I've replaced it with a new, more focused answer, but I left the old answer at the bottom for anyone still looking for it.

New answer:

This error is caused by the fact that the last time you had a server running, you closed the terminal without killing the server. I believe this is called 'running headless', like a chicken with its head cut off. So even though nobody is around watching, the server is still running and taking up the 'space' called port 9393. When you try to start a new server, there is already one running. It's kind of like a parking spot: since there is already a car there, you can't part a new one in the same spot.

Here's how I reproduced the error. I booted up a sinatra server, closed down the terminal without killing the server first, opened up a new terminal, and tried to boot up another server.

Taras-MacBook-Air:SurveyDBCGroupProject tlroys$ shotgun == Shotgun/Thin on http://127.0.0.1:9393/ Thin web server (v1.6.1 codename Death Proof) Maximum connections set to 1024 Listening on 127.0.0.1:9393, CTRL+C to stop /Users/tlroys/.rvm/gems/ruby-1.9.3-p484/gems/eventmachine-1.0.3/lib/eventmachine.rb:526:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError) #deleted stack trace

What do you do if a car is parked in your spot? You can call the tow truck and tell the driver the license plate number of the car, and tell them to tow it away.

To find out the license plate number of the car, run the following set of commands I found on Stack Overflow

ps aux | grep ruby

This finds the process id, aka the license plate number of the 'car' occupying the 'parking spot.' Note: the server occupying my 'spot' is in fact a server written using the programming language ruby: sort of like some cars are Chevorlets. I can tell the person finding out the license plate number to look for the chevrolet, and he will find the right car as long as there are no other cars around. Since this 'zombie server' is the only ruby process running on my computer, telling the grep command to look for ruby will give the right process id/ license plate numbers. If I wanted to be more specific, I could probably say

ps aux | grep shotgun

and get the same result.

-The output should look like this: there should be two things in the list

27235 ?? S 0:00.72 /Users/tlroys/.rvm/gems/ruby-1.9.3-p484/bin/shotgun 27393 s000 S+ 0:00.00 grep ruby

The first one is the actual 'zombie server' you are looking for. The second thing is funny, because you are looking for all processes that contains the word ruby, and so it find the process that is looking for all processes that contain the word ruby. Haha.

Kill the first process. with the following command. Make sure to change the numbers to the actual process id:

kill -9 27235

You'll have to change the 27235 to the actual process id that you see, but you get the idea. The tow truck has come, dragged the car away to the junkyard, and left the spot free for me to use.

Old answer:

I had exactly the same issue, and as far as I can tell it was because there was a ruby process running on the port that I run my sinatra apps on. Below is the error (With the stack trace remove for brevity's sake) and the commands I ran to figure out what was going wrong and fix it.

In short, I tried to start my sinatra app, and it said port is in use or required root privileges. I then used the Tin Man's lsof command (seen above) to find out what ports were in use.

It showed that ruby was running on port 9393. I killed it with killall. I tried starting my sinatra app again. It worked.

Taras-MacBook-Air:sinatra_sandbox tlroys$ bundle exec shotgun config.ru 
The source :rubygems is deprecated because HTTP requests are insecure.
Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
== Shotgun/Thin on http://127.0.0.1:9393/
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 127.0.0.1:9393, CTRL+C to stop
/Users/tlroys/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/eventmachine-1.0.0/lib/eventmachine.rb:526:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)
Taras-MacBook-Air:sinatra_sandbox tlroys$ lsof -i TCP | grep LISTEN
ruby      59176 tlroys    9u  IPv4 0xffffff8012fbdc00      0t0  TCP localhost:9393 (LISTEN)
Taras-MacBook-Air:sinatra_sandbox tlroys$ killall ruby
Taras-MacBook-Air:sinatra_sandbox tlroys$ bundle exec shotgun config.ru 
The source :rubygems is deprecated because HTTP requests are insecure.
Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
Shotgun/Thin on http://127.0.0.1:9393/
Thin web server (v1.5.0 codename Knife)
Maximum connections set to 1024
Listening on 127.0.0.1:9393, CTRL+C to stop
Community
  • 1
  • 1
Tara Roys
  • 1,756
  • 2
  • 17
  • 31
  • I've gone this through this routine dozens of time, but had forgotten the details between today and the last time I did this, thanks for the concrete car-towing example. – Strand McCutchen Sep 16 '15 at 04:22
1

The restriction about only root being able to open "well known port#s" has nothing to do with Ruby - it's an OS thing. It's also, in general, a Good Thing.

Look at "cannot start sinatra process - eventmachine 'no acceptor'".

There are two suggestions in the link:

  1. The configuration issue he encountered might well fix your problem
  2. If nothing else, the link also shows you how to change the port# (to some different - and perhaps higher) number.
Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • thanks, I put the set bind: localhost and set port: 9876 in the file and was able to run it. I didn't understand the instructions at the link for changing the config. not enough detail. If you understand it and can elaborate would be greatly appreciated. – BrainLikeADullPencil Dec 27 '12 at 06:25
  • yesterday I was playing around with this Rack tutorial which had us explicitly set a different port number, so I'm wondering if that changed something permanently that I don't know how to unset http://rubylearning.com/blog/a-quick-introduction-to-rack/ – BrainLikeADullPencil Dec 27 '12 at 06:30
  • @BrainLikeADullPencil use the lsof command from the comments to the question to see if anything is running on a particular port. If it is, then you've likely got another terminal window open and running a Rack instance there on that port (happens to me all the time). The tutorial specifies the port change within an IRB session, so as long as the IRB session is closed the port will be released, there should be no permanent change. – ian Dec 30 '12 at 15:01