312

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.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

13 Answers13

780

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
idlefingers
  • 31,659
  • 5
  • 82
  • 68
  • 5
    could you please explain *-wni* – user993563 Aug 04 '12 at 20:25
  • 62
    Just to clarify for novices: in the second line of code, you are supposed to replace the `PID` with the actual number that is shown in your console upon entering the first line of code (eg, `12345`). – CodeBiker Aug 07 '13 at 18:48
  • 6
    You can also find the PID by navigating through your rails directory to the following file tmp/pids/server.pid – tandy Feb 15 '14 at 19:29
  • 1
    This can happen if you're running debugger in RubyMine and it crashes or something... The server stops but the debugger continues... Anyway, this is the solution I used and it works perfectly :) – TheRealJimShady Jun 22 '15 at 12:24
  • @tandy this is a good way of getting the PID for a specific project however, sometimes when a process suddenly closes, the file might be absent but the server could still be running. Doing a `lsof -wni tcp:3000` will *always* show *all* processes listening on that protocol:port combination. (where 3000 is to be replaced with your local server port. e.g. in Rails this is 3000 by default). – SidOfc Jun 13 '17 at 15:08
  • Please don't use `kill -9` (`SIGKILL`) habitually. Use it when needed, but otherwise the default signal (`SIGTERM`) should be fine, and allows a process to clean up if/what it needs to. (`SIGKILL` forces it to die immediately, with no chance to, for example, finish writing a file, or the like.) I see no information in this question that indicates that the process isn't responding to `SIGTERM`. – lindes Dec 18 '17 at 00:02
  • 1
    On MacOS you can also do this in Activity Monitor by looking for the 'ruby' process name and clicking the x icon – nateM May 20 '20 at 21:16
170

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

Bijan
  • 6,675
  • 6
  • 33
  • 29
  • 4
    everything between the parentheses will return a process id that using the port 3000. And `-t` means `specifies that lsof should produce terse output with process identifiers only and no header - e.g., so that the output may be piped to kill(1).` than I guess you know what `kill` will do – Papouche Guinslyzinho Feb 04 '15 at 03:33
  • This should be the accepted answer as it is an 'all in one' line. Put this in an alias and you're good to go. – Nubtacular Jan 27 '16 at 21:08
  • If an explanation were included, this would definitely be worth an upvote. – mwfearnley Oct 19 '16 at 09:35
  • 3
    This will kill all processes including all opened browser session. So you want to be careful when you try it out. – theterminalguy Nov 01 '16 at 15:09
  • 1
    Please see my comment on the accepted answer about using `kill -9` by rote/habitually. – lindes Dec 18 '17 at 00:02
  • Thanks @lindes. The only time I use kill -9 on rails server is when my puma server freezes and I'm forced to `kill -9`. – Bijan Jan 18 '18 at 19:00
  • All other answers should be deleted and only have this one. I use it HABITUALLY ;D – fatfrog Jan 21 '21 at 20:29
42

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)
Shahzad Tariq
  • 2,767
  • 1
  • 22
  • 31
18

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.

saneshark
  • 1,243
  • 13
  • 25
  • 1
    This will not work on the mac (Mac was stipulated in the question) as fuser only accepts c, f, and u as options. – Toby Jun 12 '13 at 09:17
  • I have updated the answer and it addresses how to use it for mac users as well. There is no reason to down vote especially considering others , including myself stumbled upon this question in the past despite not being on a mac. – saneshark Nov 01 '13 at 15:02
  • Adding a `-v` flag also shows a small amount of info about what process got killed. So `fuser -kv 3000/tcp`. Alas that MacOS's fuser doesn't have this ability. `kill $(lsof -ti tcp:3000)` is a fairly close equivalent, though. – lindes Dec 18 '17 at 00:11
17

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

devudilip
  • 1,270
  • 14
  • 25
13

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
Lorenzo Sinisi
  • 450
  • 5
  • 8
  • this is neat. Looking at the source, your gem looks for the pid file, but that's not guaranteed to exist. the `lsof` approach is more dependable. – max pleaner Dec 08 '16 at 20:41
  • @maxpleaner: true, and also more likely to kill some other process that may be listening on that port, in case that was done (perhaps by accident). – lindes Dec 18 '17 at 00:08
  • It works here. If you are reading this in 2019, please install this gem, it really works, so much easier.. just run gem install shutup in your folder, and everytime you need to kill just type "shutup" – Guilherme Nunes Jan 02 '19 at 09:10
  • I installedl this gem, and ran it and it killed Sinatra. However, from now the default port 4567 doesn't respond, nor any other port set by the 'set :port XXXX' command. How can I handle this? – NevD Nov 05 '19 at 23:35
5

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

Sarwan Kumar
  • 1,283
  • 9
  • 24
4

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

averydev
  • 5,717
  • 2
  • 35
  • 35
3

Using this command you can kill the server:

ps aux|grep rails 
Viktor
  • 2,623
  • 3
  • 19
  • 28
Rocker
  • 115
  • 3
  • 14
3

One line solution:

kill -9 $(ps aux | grep 'rails s' | awk {'print$2'}); rails s
barbsan
  • 3,418
  • 11
  • 21
  • 28
0

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
Simon
  • 723
  • 8
  • 14
-4

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

InsanelyADHD
  • 548
  • 5
  • 11
-6

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

Community
  • 1
  • 1
Prabhakar
  • 6,458
  • 2
  • 40
  • 51
  • 3
    This will not stop the server. – Nafaa Boutefer Mar 15 '15 at 13:20
  • Although this code may be help to solve the problem, providing additional context regarding _why_ and/or _how_ it answers the question (in particular, why you think a suggestion for a Windows machine might be useful for a question that states that it's on MacOS) would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Jun 29 '16 at 11:45