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
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
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:
The configuration issue he encountered might well fix your problem
If nothing else, the link also shows you how to change the port# (to some different - and perhaps higher) number.
Related
I am running a puma server ruby application on fedora 32. In my server I have certain calls which will spawn new long running processes for various reasons. I came across an issue where my spawned processes were running and listening on the same port as my server. This lead to issues with restarting my server on deploys as the server could not start because of processes listening on the desired port. How could this be possible? From my understanding when I spawn a process it should have completely different memory to the parent process, and share no file descriptors. My spawn command is simply
my_pid = Process.spawn(my_cmd, %i[out err] => log_file)
Ruby version 2.7.0
Edit: something I had overlooked in my deploy process and my original problem description, server restart is not an actual tear down and restart of a new process, but via signalling USR2 to the puma server (as described here)
A quick workaround / solution will be to call fork, close Puma's socket within the forked process and then call exec, which replaces the running process... however, this workaround is limited to Unix systems. On windows you can probably achieve something similar using a more complicated approach.
Sadly, I am not sure how to close Puma's listening socket. Perhaps this will helps, but more likely than not there's some other trick to this.
I believe I have found what is causing this. Seems to be an issue with puma restart process, which I was using. By restarting the server with a USR2 signal, it changes the flags on the open fd for the socket.
[me#home puma_testing]$ cat /proc/511620/fdinfo/5
pos: 0
flags: 02000002
mnt_id: 10
[me#home puma_testing]$ kill -s USR2 511620
[me#home puma_testing]$ cat /proc/511620/fdinfo/5
pos: 0
flags: 02
mnt_id: 10
This was tested on fedora 32 using a very simple puma and sinatra setup like so:
puma.rb
# frozen_string_literal: true
rackup File.join(File.dirname(File.realpath(__FILE__)), './server.ru')
# https://www.rubydoc.info/gems/puma/Puma/DSL#prune_bundler-instance_method
# This allows us to install new gems with just a phased-restart. Otherwise you
# need to take the master process down each time.
prune_bundler
port 11111
environment 'production'
pidfile File.join(File.dirname(File.realpath(__FILE__)), '../', 'server.pid')
tag 'test'
And server.ru like so
require 'sinatra'
class App < Sinatra::Base
get "/" do
"Hello World!"
end
get "/spawn" do
spawn "sleep 500"
end
end
run App
Ran using bundler bundle exec puma -C puma.rb. Note you can use /spawn get request to test spawning a new process before and after restart to see if it is listening on the socket with lsof -itcp:11111
I am developing a static site locally. To view it in a browser, I run this command
ruby -run -ehttpd . -p8000
to run a local webserver at localhost:8000.
Starting yesterday, when I run it, I get the error
INFO WEBrick 1.3.1
INFO ruby 2.0.0 (2015-12-16) [universal.x86_64-darwin16]
WARN TCPServer Error: Address already in use - bind(2)
INFO WEBrick::HTTPServer#start: pid=1158 port=8000
So I change the port number -p8000 to p8001 and I get the same error. I try 8002, 8003, 8888, 1313, 8004. I the same error on every single port number. Ruby, or Webrick, thinks that every port is already in use.
All the solutions to this problem I can find online suggest finding whatever process is hogging the port using commands like lsof | grep '8000' or lsof -wni tcp:8000 and then killing that process. But those commands don't return anything. There are no processes using those ports.
This happens on a fresh restart of my machine. Wifi turned off.
What could possibly be wrong? I run this file:
require 'sinatra'
get '/' do
"Just Do It"
end
Server starts up:
== Sinatra (v2.0.0) has taken the stage on 3000 for development with backup from Thin
Thin web server (v1.7.1 codename Muffin Mode)
Maximum connections set to 1024
Listening on 127.0.0.1:3000, CTRL+C to stop
(I set the address and port because it is what I have in my vagrantfile.
However I get nothing, either in Chrome or Firefox. I've tried various config.ru's and changed the file above to an ru extension to try rackup but that hasn't worked either.
I have uninstalled and reinstalled the Mustermann and Sinatra gems and made sure I had the gems that are required in base.rb.
Any ideas?
I searched the internet for a "Hello, World" type program for Webrick in Ruby, but could not find anything that worked. I found this guide on SO but for the life of me could not get it to work.
Consulting the Ruby Documentation for Webrick led me to some code snippets that got me going in the right direction. There were no easy-to-follow tutorials so I wanted to add my answer on SO.
I was using Ubuntu 14.04 without Apache or Nginx and wanted my server for a virtual host. Webrick by default does not respond to requests concurrently but for me this was a plus in addition to its small footprint. I was hoping to get it working without the Rails framework for an even lighter footprint.
To get started, I installed Ruby with the Ubuntu package manager. If you are using CentOS or another Linux distribution, you can adapt this step to your particular package manager. Also make sure port 80 is open on your web server. It's possible to get SSL with Webrick but I chose not to at this point.
sudo apt-get install ruby
Here is the script which I named myapp.rb that I am using. I placed it /var/www/myapp. Ideally, I think it should not be in document root. You should also create a special user and group just to run the script to improve security (I have not outlined those steps here)
require 'webrick'
server = WEBrick::HTTPServer.new(:Port => 80,
:SSLEnable => false,
:DocumentRoot => '/var/www/myapp',
:ServerAlias => 'myapp.example.com')
server.mount_proc '/' do |req, res|
res.body = 'Hello, world!'
end
trap 'INT' do server.shutdown end
server.start
The require statement above tells Ruby to include the Webrick classes when running the program. The second line of the script creates an instance of Webrick with the following options:
Use Port 80
Disable SSL
Set the document root to /var/www/myapp
Set the Server Alias to myapp.example.com
Of course, you'll have to configure your particular domains DNS'. The server.mount_proc is telling Ruby to serve the response, "Hello, world" at document root. I think you can specify a subdirectory there if you life. The Ruby Webrick documentation above has information on that.
The line that begins with trap means that the web server can be stopped with Ctrl-C. To start the script I typed the following at the SSH command line:
ruby myapp.rb
I have a Sinatra app that I run as a daemon, using Apache port-forwarding to mediate between port 80 and port 7655. This has been working fine in the past. Today, not so well. I cannot figure out why.
Problem: sudo ruby my_process.rb returns:
/var/lib/gems/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:526:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)
Tried: updating all system packages, updating all gems. No help (except for the more clear error message from eventmachine).
When I run sudo lsof -i :7655 I get nothing back. When I run sudo ps aux I don't see any Ruby processes at all. Which I find highly irregular, given the nature of the error message!
So is there something I'm missing in finding out why the port is unavailable?
Also:
Tried changing ports, nothing. I wonder if it is related to "localhost"? When I ping localhost I get all dropped packets. That doesn't seem normal.
Turns out these two lines in the main Sinatra script provided the most information:
set bind: "localhost"
set port: 7655
The problem was with localhost. The loopback interface was not properly configured. ifconfig showed the lo interface, but it hadn't been assigned the IP 127.0.0.1. To resolve, ran the following commands in the shell (on an Ubuntu Linux system):
ifdown lo
ifup lo
When you close term sometime you forget to stop [CTRL+C] the actual server, just run the following command to kill all ruby process like sinatra, and run-it again
killall ruby
You can see your actual ruby process by running
ps -ef | grep ruby