151

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?

4
  • 2
    => Ctrl-C to shutdown server Commented Feb 26, 2013 at 11:39
  • @Newben do u want to kill all the process?
    – Learner
    Commented Feb 26, 2013 at 11:52
  • @Sri, yes I want to kill all the process
    – epsilones
    Commented Feb 26, 2013 at 11:54
  • 1
    @Newben Check my updated answer below and give a try and let me know
    – Learner
    Commented Feb 26, 2013 at 11:58

27 Answers 27

356

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

9
  • Sorry, lsof -wni tcp:3000 returns nothing, and I don't have any tmp/pids/ folder
    – epsilones
    Commented Feb 26, 2013 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
    Commented Feb 26, 2013 at 12:02
  • When I run killall -9 rails, I get the message telling 'no such process found ' ??
    – epsilones
    Commented Feb 26, 2013 at 12:04
  • Btw, the folder {APP_ROOT}/tmp/pids/exists indeed but it is empty
    – epsilones
    Commented Feb 26, 2013 at 12:06
  • 1
    None of these commands did anything for me. However, rebooting the machine fixed the problem. Commented Jun 2, 2015 at 22:27
57

you can use grep command in following way,

ps aux | grep rails

and then

kill -9 {process_id} 
5
  • in fact, this commnad only catches '0:00 grep --color=auto rails', which I guess is the grep called command...
    – epsilones
    Commented Feb 26, 2013 at 11:54
  • Thanks this solution working for server that still run even the terminal force closed. Nice
    – ksugiarto
    Commented Nov 14, 2013 at 3:01
  • And what if I have many processes and I don't want to kill them manually one by one? Commented Jun 15, 2015 at 12:30
  • You can kill them all by killall -9 rails Commented Jun 15, 2015 at 13:30
  • to find the server I had to do ps aux | grep puma Commented May 20, 2020 at 10:05
33

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.

0
29

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

3
  • 1
    Same. That's the COMMAND value, too.
    – IIllIIll
    Commented Apr 6, 2015 at 0:46
  • This is the only solution that worked for me, on mac as well and in a container
    – ahrampy
    Commented Mar 24, 2023 at 21:37
  • this is also the only solution on WSL.
    – Lam Phan
    Commented Aug 3, 2023 at 10:47
23

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

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.

1
  • 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
    Commented May 9, 2020 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

6

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

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
2
  • even u restarted sometimes the server is running. Because we need to kill the temp/pid to stop the server
    – Learner
    Commented Feb 26, 2013 at 11:44
  • But normally, terminating the terminal stops the process bcoz its foreground process and runs on terminal. Commented Feb 26, 2013 at 11:46
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

3

Ctrl-Z should normally do the trick.

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
3

it's as simple as

pkill -9 ruby

nothing more nothing less

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

2

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

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.

1

check the /tmp/tmp/server.pid

there is a pid inside.

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

2
  • I don't have this path, where could it be on an ubuntu machine ?
    – epsilones
    Commented Feb 26, 2013 at 11:53
  • it is hidden...go to your app folder,ctrl + H to show your hidden files ,then u able to see it
    – Nich
    Commented Feb 26, 2013 at 12:05
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?

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.

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
2
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}

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`
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.

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
0

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

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

-3

Press Ctrl - C it will stop
if not check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.