Rails server says port already used, how to kill that process?

Ruby on-Rails

Ruby on-Rails Problem Overview


I'm on a mac, doing:

rails server

I get:

2010-12-17 12:35:15] INFO  WEBrick 1.3.1
[2010-12-17 12:35:15] INFO  ruby 1.8.7 (2010-08-16) [i686-darwin10.4.0]
[2010-12-17 12:35:15] WARN  TCPServer Error: Address already in use - bind(2)
Exiting

I know I can start one on a new port, but I want to kill this process.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Assuming you're looking to kill whatever is on port 3000 (which is what webrick normally uses), type this in your terminal to find out the PID of the process:

$ lsof -wni tcp:3000

Then, use the number in the PID column to kill the process:

$ kill -9 PID

Solution 2 - Ruby on-Rails

kill -9 $(lsof -i tcp:3000 -t)

Solution 3 - Ruby on-Rails

You need to get process id of program using tcp port 3000. To get process id

lsof -i tcp:3000 -t

And then using that process id, simply kill process using ubuntu kill command.

kill -9 pid

Or just run below mentioned combine command. It will first fetch pid and then kill that process.

kill -9 $(lsof -i tcp:3000 -t)

Solution 4 - Ruby on-Rails

For anyone stumbling across this question that is not on a Mac: assuming you know that your server is running on port 3000, you can do this in one shot by executing the following:

fuser -k 3000/tcp

But as Toby has mentioned, the implementation of fuser in Mac OS is rather primitive and this command will not work on mac.

Solution 5 - Ruby on-Rails

Some times there is a chance where rails server not closed properly. You can find process used by rails

> ps aux | grep rails

Output will be like

user     12609  9.8  0.5  66456 45480 pts/0    Sl+  21:06   0:02 /home/user/.rvm/rubies/ruby-2.2.0-preview1/bin/ruby bin/rails s

Here process_id 12609 is used by your rails server.

You can kill it easily by command

> kill -9 12609

Solution 6 - Ruby on-Rails

All the answers above are really good but I needed a way to type as little as possible in the terminal so I created a gem for that. You can install the gem only once and run the command 'shutup' every time you wanna kill the Rails process (while being in the current folder).

gem install shutup

then go in the current folder of your rails project and run

shutup # this will kill the Rails process currently running

You can use the command 'shutup' every time you want

DICLAIMER: I am the creator of this gem

NOTE: if you are using rvm install the gem globally

rvm @global do gem install shutup

Solution 7 - Ruby on-Rails

Using this command you can kill the server:

ps aux|grep rails 

Solution 8 - Ruby on-Rails

One line solution:

kill -9 $(ps aux | grep 'rails s' | awk {'print$2'}); rails s

Solution 9 - Ruby on-Rails

By default, rails server uses port 3000.
So, you have 2 options to run rails server.

  1. Either you can run the server on other port by defining custom port using the following command
    rails s -p 3001
  2. Or you can kill all the running ruby process by running following command
    killall -9 ruby
    then run rails server

Solution 10 - Ruby on-Rails

So this is a script for WSL that kills processes in windows

PIDS=$(/mnt/c/windows/system32/cmd.exe /c netstat -ano | /mnt/c/windows/system32/cmd.exe /c findstr :$1 | awk '{print $5}')
for pid in $PIDS
do
    /mnt/c/windows/system32/cmd.exe /c wmic process where "processid=$pid" delete
done

example

myscriptname 8080

Solution 11 - Ruby on-Rails

Type in:

man lsof

Then look for -w, -n, and -i

-i: internet stuff -n: makes it faster -w: toggles warnings

There are WAY more details on the man pages

Solution 12 - Ruby on-Rails

If you are on windows machine follow these steps.

c:/project/
cd tmp
c:/project/tmp
cd pids
c:/project/tmp/pids
dir

There you will a file called server.pid

delete it.

c:/project/tmp/pid> del *.pid

Thats it.

EDIT: Please refer this

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - Ruby on-RailsidlefingersView Answer on Stackoverflow
Solution 2 - Ruby on-RailsBijanView Answer on Stackoverflow
Solution 3 - Ruby on-RailsShahzad TariqView Answer on Stackoverflow
Solution 4 - Ruby on-RailssanesharkView Answer on Stackoverflow
Solution 5 - Ruby on-RailsdevudilipView Answer on Stackoverflow
Solution 6 - Ruby on-RailsLorenzo SinisiView Answer on Stackoverflow
Solution 7 - Ruby on-RailsRockerView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAnmol ChawlaView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSarwan KumarView Answer on Stackoverflow
Solution 10 - Ruby on-RailsSimonView Answer on Stackoverflow
Solution 11 - Ruby on-RailsInsanelyADHDView Answer on Stackoverflow
Solution 12 - Ruby on-RailsPrabhakar UndurthiView Answer on Stackoverflow