149

I am new to rails and I am using an ubuntu machine and the rubymine IDE. The problem is that I am unable to stop the rails server. I tried to stop the server by killing the rails process. But, when I run pgrep -l rails, no such process is found. So, I am only able to kill ruby processes, but, the server won't stop.

I tried ./script/server stop (since I started it by running ./script/server start), but, that didn't work. Googling around and finding some stackoverflow posts, I tried to change the localhost port's listening port but without success. Could someone help?

whyceewhite
  • 6,317
  • 7
  • 43
  • 51
epsilones
  • 11,279
  • 21
  • 61
  • 85

27 Answers27

354

You can use other ports like the following:

rails server -p 3001

Normally in your terminal you can try Ctrl + C to shutdown the server.

The other way to kill the Ruby on Rails default server (which is WEBrick) is:

kill -INT $(cat tmp/pids/server.pid)

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:

For example:

$ kill -9 PID

And some of the other answers i found is:

To stop the rails server while it's running, press:

CTRL-C
CTRL-Z

You will get control back to bash. Then type (without the $):

$ fg

And this will go back into the process, and then quit out of Rails s properly.

It's a little annoying, but this sure beats killing the process manually. It's not too bad and it's the best I could figure out.

Updated answer:

You can use killall -9 rails to kill all running apps with "rails" in the name.

killall -9 rails

Learner
  • 4,596
  • 1
  • 20
  • 23
  • Sorry, lsof -wni tcp:3000 returns nothing, and I don't have any tmp/pids/ folder – epsilones Feb 26 '13 at 11:56
  • If Webrick is running, then its PID is in `{APP_ROOT}/tmp/pids/server.pid` file so you don't have to look for it -- as long as server is running. So, if instead of doing `ctrl-c` you just run that kill command in another terminal, it will kill Webrick server immediately. – Learner Feb 26 '13 at 12:02
  • When I run killall -9 rails, I get the message telling 'no such process found ' ?? – epsilones Feb 26 '13 at 12:04
  • Btw, the folder {APP_ROOT}/tmp/pids/exists indeed but it is empty – epsilones Feb 26 '13 at 12:06
  • try `fuser -n tcp 3000` and kill the process listed if any. Then restart the server..or u can start the server in another port `ruby script/server -p 3001` – Learner Feb 26 '13 at 12:07
  • If it's empty then you can start the rails server as you do normally to check... and try to restart the machine once to get the better results... sometimes its will work... – Learner Feb 26 '13 at 12:09
  • 1
    None of these commands did anything for me. However, rebooting the machine fixed the problem. – Herb Meehan Jun 02 '15 at 22:27
  • I had to delete (`rm`) the pid file. [See this answer](https://stackoverflow.com/a/24627702/1494454). – totymedli Aug 27 '19 at 13:07
  • By my, the prosses is called ruby not rails so `killall -9 ruby` did the job – Arye Eidelman Sep 06 '19 at 22:06
55

you can use grep command in following way,

ps aux | grep rails

and then

kill -9 {process_id} 
maximus ツ
  • 7,949
  • 3
  • 25
  • 54
34

pkill -9 rails to kill all the process of rails

Updated answer

ps aux|grep 'rails'|grep -v 'grep'|awk '{ print $2 }'|xargs kill -9

This will kill any running rails process. Replace 'rails' with something else to kill any other processes.

Patsy Issa
  • 11,113
  • 4
  • 55
  • 74
Vasu Adari
  • 910
  • 9
  • 14
29

On my MAC the killall -9 rails does not work. But killall -9 ruby does.

Severin
  • 8,508
  • 14
  • 68
  • 117
user2055780
  • 321
  • 3
  • 4
22

Following are steps to kill server process:

1. lsof -i tcp:3000

2. kill -9 1234

where 1234 is the PID of process: localhost:3000 display in step 1.

OR

Remove file(server.pid) under Rails.root/tmp/pids/ and restart server.

OR

open app in another port by using command:

rails s -p 3001

puneet18
  • 4,341
  • 2
  • 21
  • 27
11

I generally use:

killall ruby

OR

pkill -9 ruby

which will kill all ruby related processes that are running like rails server, rails console, etc.

przbadu
  • 5,769
  • 5
  • 42
  • 67
  • If you don't know pkill -9 or kill -9 , it might also be worth to notice that this could kill other ruby processes as well, for example the rubymine IDE. – Kjell May 09 '20 at 10:32
8

1. Simply Delete the pid file from rails app directory

Rails_app -> tmp -> pids -> pid file

Delete the file and run

rails start


2. For Rails 5.0 and above, you can use this command

rails restart

Aravin
  • 6,605
  • 5
  • 42
  • 58
5

If you are using a more modern version of Rails and it uses Puma as the web server, you can run the following command to find the stuck Puma process:

ps aux | grep puma

It will result in output similar to this:

85923 100.0  0.8  2682420 131324 s004  R+    2:54pm   3:27.92 puma 3.12.0 (tcp://0.0.0.0:3010) [my-app]
92463   0.0  0.0  2458404   1976 s008  S+    3:09pm   0:00.00 grep puma

You want the process that is not referring to grep. In this case, the process ID is 85923.

I can then run the following command to kill that process:

kill -9 85923

enter image description here

Tina
  • 1,186
  • 10
  • 11
3

Use ctrl+c to shutdown your Webrick Server.

Unfortunately if its not works then forcefully close the terminal and restart it.

Another trick is that

1. open your system-monitor(a gui application) on ubuntu

2. Select processes tab 

3. Then look for a process having name 'ruby'

4. End that process
Gopal S Rathore
  • 9,885
  • 3
  • 30
  • 38
3

Delete the server.pid from tmp/pids folder. In my case, the error was: A server is already running. Check /home/sbhatta/myapp/tmp/pids/server.pid.

So, I delete server.pid

rm /home/sbhatta/myapp/tmp/pids/server.pid then run rails s

Santanu
  • 960
  • 10
  • 14
3

Ctrl-Z should normally do the trick.

himanish.k
  • 105
  • 1
  • 11
3

Step 1: find what are the items are consuming 3000 port.

lsof -i:3000

step 2 : Find the process named

For Mac

ruby      TCP localhost:hbci (LISTEN)

For Ubuntu

ruby      TCP *:3000 (LISTEN)

Step 3: Find the PID of the process and kill it.

kill -9 PID
errakeshpd
  • 2,544
  • 2
  • 28
  • 35
2

I used killall -9 rails like Sri suggested and it didn't work. I adjusted the command to killall -9 ruby and the server closed immediately.

Tl;dr: killall -9 ruby

Rebecca
  • 105
  • 8
2

it's as simple as

pkill -9 ruby

nothing more nothing less

ELTA
  • 1,474
  • 2
  • 12
  • 25
1

check the /tmp/tmp/server.pid

there is a pid inside.

Usually, I ill do "kill -9 THE_PID" in the cmd

Community
  • 1
  • 1
Nich
  • 1,112
  • 1
  • 14
  • 29
1

When the rails server does not start it means that it is already running then you can start by using new port eg.

rails s -p 3001

or it starts and stops in that case you want to delete temp folder in rails directory structure it starts the rails server.

Robert
  • 5,278
  • 43
  • 65
  • 115
Gajanan
  • 11
  • 1
1

I have noticed on Windows (Im using 10 but not sure if the same for oler). If you use cmd.exe and ctrl + c the raisl server stops correctly.

However, if you use Git Bash, it doesn't. It says it has but when you look at the tmp pids, its still there.

Maybe a bug with git bash?

Brad
  • 8,044
  • 10
  • 39
  • 50
1

killall -9 ruby will kill all the ruby processes, and at-least on my machine, rails servers appear as ruby processes. killall -9 rails is much more specific and doesn't work for older versions of rails servers (it gives a 'rails:no process found' because the process is named ruby)

Encountered this problem a while ago. After submitting a form in activeadmin, the rails server just hanged and I was unable to kill it using normal means (even after ctrl+z it was still running in the background). Learner's answer helped, but this command doesn't need process id.

1

We can kill rails session on Linux using PORT no

fuser -k 3000/tcp 

here 3000 is a port no. Now restart your server, you will see your server is in running state.

Akash Jain
  • 483
  • 5
  • 5
1

Follow these steps:

  1. open your project
  2. select in tmp folder
  3. select pids folder
  4. delete server.pid file
  5. now start your rails server
Cœur
  • 37,241
  • 25
  • 195
  • 267
1

On rails 6 using

ps aux | grep rails was not returning the server process

I had to do

ps aux | grep puma

to find the actual process and then kill it using

kill -9 {process_id}

Sami Birnbaum
  • 773
  • 8
  • 20
0

It is late for this question. Here is my 2 cents. I made a rake task for stopping the server when I don't have access to it. I only tested on Mac though.

With this you can simply add it to your project then run the rake command.

Here you go:

Gist link: -latest version will be here. https://gist.github.com/houmanka/289184ca5d8d92de0499#file-server-rake

Some code in here:

# Make a file under: `project_root/lib/tasks/server.rake`

# Then paste the following code

    namespace :server do
      desc "Stop the running server by killing the PID"
      task :kill do
        STDOUT.puts "Enter port number: "
        post_number = STDIN.gets.strip
        system "pid=$(lsof -i:#{post_number.to_i} -t); kill -TERM $pid || kill -KILL $pid"
      end
    end

# Then to use it in the terminal: `rake server:kill`
Mr H
  • 5,254
  • 3
  • 38
  • 43
0

Also, Make sure that you are doing command Cntrl+C in the same terminal (tab) which is used to start the server.

In my case, I had 2 tabs but i forgot to stop the server from correct tab and i was wondering why Cntrl+C is not working.

Ash
  • 1,391
  • 15
  • 20
0

One super easy way would be

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
0

For my windows 10 machine, Ctrl - C + Ctrl - D works.

rcs
  • 6,713
  • 12
  • 53
  • 75
Ebran Khan
  • 21
  • 2
0
  1. Just open the file using the location given sudo vi /Users/user1/go/src/github.com/rails_app/rails_project/tmp/pids/server.pid

  2. find the process_id / thread_id at which the process is runnning.

  3. Kill the specified process / thread using kill -9 84699

Soumya Sengupta
  • 105
  • 2
  • 11
-1

Press Ctrl - C it will stop
if not check