Can't stop rails server

Ruby on-Rails

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

you can use grep command in following way,

ps aux | grep rails

and then

kill -9 {process_id} 

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

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

Solution 9 - Ruby on-Rails

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

Solution 10 - Ruby on-Rails

Ctrl-Z should normally do the trick.

Solution 11 - Ruby on-Rails

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

Solution 12 - Ruby on-Rails

it's as simple as

pkill -9 ruby

nothing more nothing less

Solution 13 - Ruby on-Rails

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

Solution 14 - Ruby on-Rails

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

Solution 15 - Ruby on-Rails

check the /tmp/tmp/server.pid

there is a pid inside.

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

Solution 16 - Ruby on-Rails

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.

Solution 17 - Ruby on-Rails

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?

Solution 18 - Ruby on-Rails

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.

Solution 19 - Ruby on-Rails

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`

Solution 20 - Ruby on-Rails

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.

Solution 21 - Ruby on-Rails

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

Solution 22 - Ruby on-Rails

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

Solution 23 - Ruby on-Rails

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.

Solution 24 - Ruby on-Rails

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

Solution 25 - Ruby on-Rails

  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

Solution 26 - Ruby on-Rails

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}

Solution 27 - Ruby on-Rails

Press Ctrl - C it will stop
if not check

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
QuestionepsilonesView Question on Stackoverflow
Solution 1 - Ruby on-RailsLearnerView Answer on Stackoverflow
Solution 2 - Ruby on-Railsmaximus ツView Answer on Stackoverflow
Solution 3 - Ruby on-RailsVasu AdariView Answer on Stackoverflow
Solution 4 - Ruby on-Railspuneet18View Answer on Stackoverflow
Solution 5 - Ruby on-Railsuser2055780View Answer on Stackoverflow
Solution 6 - Ruby on-RailsprzbaduView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAravinView Answer on Stackoverflow
Solution 8 - Ruby on-RailsGopal S RathoreView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSantanuView Answer on Stackoverflow
Solution 10 - Ruby on-Railshimanish.kView Answer on Stackoverflow
Solution 11 - Ruby on-RailserrakeshpdView Answer on Stackoverflow
Solution 12 - Ruby on-RailsELTAView Answer on Stackoverflow
Solution 13 - Ruby on-RailsTinaView Answer on Stackoverflow
Solution 14 - Ruby on-RailsRebeccaView Answer on Stackoverflow
Solution 15 - Ruby on-RailsNichView Answer on Stackoverflow
Solution 16 - Ruby on-RailsGajananView Answer on Stackoverflow
Solution 17 - Ruby on-RailsBradView Answer on Stackoverflow
Solution 18 - Ruby on-RailsSomil MishraView Answer on Stackoverflow
Solution 19 - Ruby on-RailsMr HView Answer on Stackoverflow
Solution 20 - Ruby on-RailsAshView Answer on Stackoverflow
Solution 21 - Ruby on-RailsLorenzo SinisiView Answer on Stackoverflow
Solution 22 - Ruby on-RailsEbran KhanView Answer on Stackoverflow
Solution 23 - Ruby on-RailsAkash JainView Answer on Stackoverflow
Solution 24 - Ruby on-RailsKrishna Singh ShahiView Answer on Stackoverflow
Solution 25 - Ruby on-RailsSoumya SenguptaView Answer on Stackoverflow
Solution 26 - Ruby on-RailsSami BirnbaumView Answer on Stackoverflow
Solution 27 - Ruby on-RailsKürşat KarslıView Answer on Stackoverflow