Rails server says port already used, how to kill that process?
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)
开发者_Python百科Exiting
I know I can start one on a new port, but I want to kill this process.
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
kill -9 $(lsof -i tcp:3000 -t)
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)
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.
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
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
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
One line solution:
kill -9 $(ps aux | grep 'rails s' | awk {'print$2'}); rails s
Using this command you can kill the server:
ps aux|grep rails
The existing answers are great, but I found that Bijan's answer cast too wide a net, catching up processes that could be unrelated.
Here's a 1-liner that combines port as well as ruby
as a string search
kill -9 $(lsof -i tcp:3000 -t -c ruby -a)
Breaking down how it works:
kill -9
will kill processes that are listed. But first run the command within $( )
lsof -i tcp:3000
lists all processes on that port
-t
is terse mode, so just the pid
-c
searches by command, which in this case is ruby
since i'm using puma
-a
is for all
, and combines the -i
and -c
together
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
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
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
精彩评论