Rails: Address already in use - bind(2) (Errno::EADDRINUSE) - ruby-on-rails

I am trying to deploy Rails app with the Puma web server. When trying to start Puma server with a config file bundle exec puma -C config/puma.rb I get an error that the address is already in use.
Does someone know how to fix this?
bundle exec puma -C config/puma.rb
[23699] Puma starting in cluster mode...
[23699] * Version 2.11.3 (ruby 2.0.0-p353), codename: Intrepid Squirrel
[23699] * Min threads: 5, max threads: 5
[23699] * Environment: development
[23699] * Process workers: 2
[23699] * Preloading application
Jdbc-MySQL is only for use with JRuby
[23699] * Listening on tcp://0.0.0.0:3000
/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `new'
from /Users/lexi87/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `add_tcp_listener'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:96:in `block in parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/runner.rb:119:in `load_and_bind'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cluster.rb:302:in `run'
from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
from /rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `load'
from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `<main>'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'

You need to use kill -9 59780 with 59780 replaced with found PID number (use lsof -wni tcp:3000 to see which process used 3000 port and get the process PID).
Or you can just modify your puma config change the tcp port tcp://127.0.0.1:3000 from 3000 to 9292 or other port that not been used.
Or you can start your rails app by using:
bundle exec puma -C config/puma.rb -b tcp://127.0.0.1:3001

To kill the puma process first run
lsof -wni tcp:3000
to show what is using port 3000. Then use the PID that comes with the result to run the kill process.
For example after running lsof -wni tcp:3000 you might get something like
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 3366 dummy 8u IPv4 16901 0t0 TCP 127.0.0.1:3000 (LISTEN)
Now run the following to kill the process. (where 3366 is the PID)
kill -9 3366
Should resolve the issue

you can also try this trick:
ps aux | grep puma
sample output:
myname 77921 0.0 0.0 2433828 1972 s000 R+ 11:17AM 0:00.00 grep puma
myname 67661 0.0 2.3 2680504 191204 s002 S+ 11:00AM 0:18.38 puma 3.11.2 (tcp://localhost:3000) [my_proj]
then:
kill -9 67661

Found the script below in this github issue. Works great for me.
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
You can either run it in irb or inside a ruby file.
For the latter, create server_killer.rb then run it with ruby server_killer.rb

SOLUTION FOR GENERAL Address already in use - bind(2) (Errno::EADDRINUSE)
This issue is because we are trying to use the same port which is already is use. so we have to stop the services running on that port so that we can run another services.
we can use kill like kill -9 {PID}where {PID} is the PID of the services running on that port. To know the PID of any services lets say "firefox" we can use commands like pidof firefox, ps aux | grep -i firefox ,pgrep firefox and then use the kill command to stop that service.
sometime we might get into the situation where we don't know the PID or the service name to search for in this case we can use the following little ruby code to do it for us.(in this case port 3000, you can change it according to your need)
#!/usr/bin/env ruby
port = ARGV.first || 3000
system("sudo echo kill-server-on #{port}")
pid = `sudo lsof -iTCP -sTCP:LISTEN -n -P | grep #{port} | awk '{ print $2 }' | head -n 1`.strip
puts "PID: #{pid}"
`kill -9 #{pid}` unless pid.empty?
save it as something.rb and run sudo ruby something.rb

If the above solutions don't work on ubuntu/linux then you can try this
sudo fuser -k -n tcp port
Run it several times to kill processes on your port of choosing. port could be 3000 for example. You would have killed all the processes if you see no output after running the command

You can find and kill the running processes: ps aux | grep puma
Then you can kill it with kill PID

I had this issue on my Macbook Air, running Rails 5.0.3, Puma 5.2.2
Tried running
lsof -wni tcp:3000 but there's no process on this port number.
Managed to fix this by running:
export PORT=3000 on my terminal, then I just added this extra line to my .bash_profile

It might be old but in my case, it was because of docker. Hope it will help others.

Related

How to stop a nohup process running rails server on port 3000

I have started a rails server puma by using the following command.
nohup rails server &
its output was [2] 22481 along with the following:
nohup: ignoring input and appending output to 'nohup.out'
But now I have forget the returned process id, so how can I detect the process id so as to delete the process on aws.
To kill whatever is on port 3000 (webrick server default port), type this below command to get process id for 3000 port:
$ lsof -wni tcp:3000
Then, use process id (PID) to kill the process:
$ kill -9 PID
Rails server process pid can be found in this directory:
-> tmp/pids/server.pid
then,
Kill -9 pid
command
ps -ef
return the full output list of processes in which one of the list item is as:
ec2-user 12992 1 0 Dec20 ? 00:00:57 puma 3.12.0 (tcp://0.0.0.0:3000) [tukatech_garmentstore_live]
so force killed the process by.
kill -9 12992
did the job
ps aux|grep 3000
This will give you rails server id running on port 3000

Cannot kill process on port 3000

I cannot seem to figure out how to kill this process.
I already know that i can, and have been, just running the server on a different port, but it's just annoying me that i can't figure this out.
Below you'll see first the error i get when i try to run rails s then all of my attempts to find the PID and kill.
// ♥ rails s
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.2 (ruby 2.2.3-p173), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Exiting
/Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:266:in `initialize': Address already in use - bind(2) for "::1" port 3000 (Errno::EADDRINUSE)
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:266:in `new'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:266:in `add_tcp_listener'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:260:in `block in add_tcp_listener'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:259:in `each'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:259:in `add_tcp_listener'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:102:in `block in parse'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:85:in `each'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/binder.rb:85:in `parse'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/runner.rb:133:in `load_and_bind'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/single.rb:85:in `run'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/puma/launcher.rb:172:in `run'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/puma-3.6.2/lib/rack/handler/puma.rb:51:in `run'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/rack-2.0.1/lib/rack/server.rb:296:in `start'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/server.rb:79:in `start'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:90:in `block in server'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/jrogers2/Development/code/presently/bin/rails:9:in `require'
from /Users/jrogers2/Development/code/presently/bin/rails:9:in `<top (required)>'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `load'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `call'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/client/command.rb:7:in `call'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/client.rb:30:in `run'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/bin/spring:49:in `<top (required)>'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `load'
from /Users/jrogers2/.rvm/gems/ruby-2.2.3/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `<top (required)>'
from /Users/jrogers2/Development/code/presently/bin/spring:14:in `require'
from /Users/jrogers2/Development/code/presently/bin/spring:14:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
[09:28:36] (master) presently
// ♥ sudo lsof -n -i4TCP:3000 | grep LISTEN
postgres 101 postgres 5u IPv4 0x97a8cfe190b174f1 0t0 TCP *:hbci (LISTEN)
[09:28:43] (master) presently
// ♥ kill -9 101
-bash: kill: (101) - Operation not permitted
[09:28:45] (master) presently
// ♥ ps aux | grep puma
jrogers2 27960 0.0 0.0 2432804 1972 s000 S+ 9:28AM 0:00.00 grep puma
[09:28:58] (master) presently
// ♥ kill -9 27960
-bash: kill: (27960) - No such process
[09:29:14] (master) presently
// ♥ ps aux | grep 3000
jrogers2 27971 0.0 0.0 2442612 1196 s000 R+ 9:29AM 0:00.00 grep 3000
[09:29:28] (master) presently
// ♥ kill -9 27971
-bash: kill: (27971) - No such process
// ♥ lsof -wni tcp:3000
[09:32:03] (master) presently
// ♥ lsof -i tcp:3000
[09:32:41] (master) presently
// ♥ ps aux | grep rails
jrogers2 28035 0.0 0.0 2442612 1172 s000 R+ 9:34AM 0:00.00 grep rails
[09:34:14] (master) presently
// ♥ kill -9 28035
-bash: kill: (28035) - No such process
find your rails s process PID and kill it
$ ps aux | grep -v grep | grep rails
$ sudo kill -9 <pid_of_rails_s_from_above>
or you can try this one liner
$ sudo kill -9 $(lsof -i tcp:3000 -t)
tmp/pids $ kill -9 $(cat server.pid)
While I run rails, I get the following output for lsof:
$ sudo lsof -n -i4TCP:3000 | grep LISTEN
ruby 23582 username 14u IPv4 0x2c5fd1f36e3c475f 0t0 TCP *:hbci (LISTEN)
So you already seem to have figured out what process is runnning on port 3000, a postgres process owned by the postgres user and probably not rails related. Given that it is running on a very low pid, 101, it was very likely started within the boot process. So instead on focussing how to kill it, I would look what caused it to start in the first place. Perhaps you should check your postrgres config (postgres.conf), was it's setup changed to run on port 3000?
If you really want to kill it, sudo is your friend:
sudo kill 101
I'd be careful with using kill -9 as there are certain risks in killing databases with that signal (SIGKILL), the process will die immediately and can't clean up after itself (more on this: GIYF, for example https://www.linuxvoice.com/core-technology-signals/. There was a good answer on signals on stackoverflow (IIRC), but can't seem to find it just now...)
ps aux | grep 3000
sudo kill -9
Which kill process force fully

Rails 5 Address already in use - bind(2) for "127.0.0.1" port 3000

After some coding got this error on running rails s:
Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)
My environment is:
$ rails -v
Rails 5.0.0
$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
I've tried:
Create a new project - the same
Checked Rails 4.2 - problem solved
Reinstall rails 5 and ruby - the same problem
lsof -wni tcp:3000 returns me nothing
ps aux | grep "rails" - nothing
ps aux | grep "puma" - nothing
ps aux | grep "ruby" -nothing
Use puma instead of rails s - problem solved
Use rails s -p 3001 - same problem, for other ports too
UPDATED
Use RAILS_ENV=production bundle exec rails s - problem solved
Any suggestions?
The same process is running somewhere
To see which process used 3000 port and get the process pid type below command
lsof -wni tcp:3000
This will give the process which is using this port
Sample result
process1 12345 0.0 0.0 12343566 1972 s000 R+ 11:17AM 0:00.00 grep puma
You can kill this process by typing below command
12345 this is the process ID
kill -9 12345
Now start server again
The problem appeared because of a bug in Puma's code. Downgrading to the oldest version helped me.
Bug ticket: https://github.com/puma/puma/issues/1022
What your error is saying is that there is something already running on port 3000. Without knowing more about your environment or what you have installed, it's impossible to know what to stop.
It's highly likely that another rails server is already running somewhere. Try going to localhost:3000 to see what's there.
It's possible that another program is piggybacking on this port for some unknown reason, if so and you can't stop it, use a differnt port
rails s -p 3001
or any other port you choose

can't open Puma server on port 3000 and can't find any processes to kill

I am running Puma on Rails 4.2.1 application.
When I run the following command to start the server:
rails s
I get the following error:
puma-3.4.0/lib/puma/binder.rb:255:in `initialize': Address already in use - bind(2) for "localhost" port 3000 (Errno::EADDRINUSE)
When I look for the process that is running to kill it:
ps aux | grep ruby
I get the following:
mitchellgould 52496 0.0 0.0 2432772 648 s000 S+ 3:34PM 0:00.01 grep ruby
When I run:
sudo kill -9 52496
I get:
kill: 52496: No such process
I can start the server in another port but I would like to remove this issue and understand what is happening. Any help appreciated.
ps aux | grep puma
or
ps aux | grep rails
then copy its pid and kill the process:
kill -9 PUMAS_PID
and run the server again
chell.
Try to check port 3000, maybe this is not ruby.
lsof -i tcp:3000
Fast solution:
You can restart your OS, all ports will be reset...
Delete pid with server (your_project/tmp/pids/server.pid)
Puma
Check your config for Puma in your project and how you start puma server.
Puma start with Rails

Doing Hart's Rails Tutorial, but server won't shut down for me fire it up after updates

I have made some changes to various files, and need to shut down and then restart the server to see them. I am using the Cloud9 railstutorial environment. But I keep getting the same error - "A server is already running". Please see below:
darrenbrett:~/workspace/sample_app (filling-in-layout) $ rails server -b $IP -p $PORT
=> Booting WEBrick
=> Rails 4.2.2 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
A server is already running. Check /home/ubuntu/workspace/sample_app/tmp/pids/server.pid.
Exiting
darrenbrett:~/workspace/sample_app (filling-in-layout) $
Find out the process id (PID) first:
$ lsof -wni tcp:8080
This will give you something like this:
$ lsof -wni tcp:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 59656 rislam 14u IPv6 0xa86b8563672ef037 0t0 TCP [::1]:http-alt (LISTEN)
Then, kill the process with PID = 59656 (for example, it will be different for you):
$ kill -9 59656
This should solve your problem.
You can also use the following command to kill all running apps that has rails in the name:
killall -9 rails
Sometimes, this is very effective when the first command does not do the trick.
It sounds like you either have an orphaned server process running, or you have multiple applications in the same environment and you're running the server for another one and it's bound to the same port. I'm guessing the former.
Run ps -fe | grep rails. You should see something like 501 35861 2428 0 1:55PM ttys012 0:04.00 /your_file_path/ruby bin/rails server
Grab the process id (in the case of the example, it's 35861), and run kill -9 35861. Then try to start your server again.
The easiest way I found to kill that server is to click on the suggested file in the line:
A server is already running. Check /home/ubuntu/workspace/sample_app/tmp/pids/server.pid
That opens the file containing a number, the process id you're looking for.
Example
43029
In the terminal window use "kill" via that same process id and restart the server.
kill -15 43029
rails server -b $IP -p $PORT

Resources