Can't stop rails server
Asked Answered
B

27

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?

Birth answered 26/2, 2013 at 11:37 Comment(4)
=> Ctrl-C to shutdown serverTwilatwilight
@Newben do u want to kill all the process?Toxicogenic
@Sri, yes I want to kill all the processBirth
@Newben Check my updated answer below and give a try and let me knowToxicogenic
T
355

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

Toxicogenic answered 26/2, 2013 at 11:42 Comment(9)
Sorry, lsof -wni tcp:3000 returns nothing, and I don't have any tmp/pids/ folderBirth
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.Toxicogenic
When I run killall -9 rails, I get the message telling 'no such process found ' ??Birth
Btw, the folder {APP_ROOT}/tmp/pids/exists indeed but it is emptyBirth
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 3001Toxicogenic
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...Toxicogenic
None of these commands did anything for me. However, rebooting the machine fixed the problem.Stilbite
I had to delete (rm) the pid file. See this answer.Scintillate
By my, the prosses is called ruby not rails so killall -9 ruby did the jobTumefy
A
57

you can use grep command in following way,

ps aux | grep rails

and then

kill -9 {process_id} 
Apyretic answered 26/2, 2013 at 11:48 Comment(5)
in fact, this commnad only catches '0:00 grep --color=auto rails', which I guess is the grep called command...Birth
Thanks this solution working for server that still run even the terminal force closed. NiceChandra
And what if I have many processes and I don't want to kill them manually one by one?Ecclesiology
You can kill them all by killall -9 rails Chasten
to find the server I had to do ps aux | grep pumaQuirinus
P
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.

Perpetual answered 1/2, 2014 at 14:18 Comment(0)
W
29

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

Wellesley answered 30/9, 2014 at 17:33 Comment(3)
Same. That's the COMMAND value, too.Ardra
This is the only solution that worked for me, on mac as well and in a containerLights
this is also the only solution on WSL.Runstadler
G
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

Gertrudgertruda answered 11/1, 2017 at 6:48 Comment(0)
B
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.

Balkan answered 1/6, 2016 at 4:39 Comment(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.Cacao
B
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

Brinna answered 31/5, 2017 at 16:35 Comment(0)
M
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

Monomolecular answered 18/4, 2020 at 22:17 Comment(0)
L
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
Lungwort answered 26/2, 2013 at 11:44 Comment(2)
even u restarted sometimes the server is running. Because we need to kill the temp/pid to stop the serverToxicogenic
But normally, terminating the terminal stops the process bcoz its foreground process and runs on terminal.Lungwort
H
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

Halfhearted answered 26/9, 2014 at 9:11 Comment(0)
W
3

Ctrl-Z should normally do the trick.

Wonky answered 24/7, 2015 at 6:28 Comment(0)
F
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
Frisket answered 17/9, 2017 at 3:43 Comment(0)
E
3

it's as simple as

pkill -9 ruby

nothing more nothing less

Englishry answered 4/11, 2018 at 16:22 Comment(0)
P
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

Prophecy answered 15/10, 2015 at 22:24 Comment(0)
X
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.

Xylol answered 30/8, 2018 at 7:9 Comment(0)
D
1

check the /tmp/tmp/server.pid

there is a pid inside.

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

Decalogue answered 26/2, 2013 at 11:46 Comment(2)
I don't have this path, where could it be on an ubuntu machine ?Birth
it is hidden...go to your app folder,ctrl + H to show your hidden files ,then u able to see itDecalogue
P
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.

Peppercorn answered 21/11, 2013 at 10:13 Comment(0)
S
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?

Spec answered 26/3, 2016 at 1:41 Comment(0)
D
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.

Dunne answered 22/2, 2019 at 7:57 Comment(0)
L
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
Lulu answered 19/7, 2019 at 6:30 Comment(2)
Didn't work on my rails 5.2 application. Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)Awake
@Awake Then use this https://mcmap.net/q/101120/-can-39-t-stop-rails-serverMacron
Q
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}

Quirinus answered 2/1, 2020 at 12:9 Comment(0)
S
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`
Showing answered 3/6, 2015 at 4:57 Comment(0)
C
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.

Coxswain answered 25/8, 2016 at 10:30 Comment(0)
N
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
Nicholle answered 16/11, 2016 at 11:59 Comment(0)
A
0

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

Accessary answered 26/8, 2018 at 17:38 Comment(0)
A
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

Advent answered 2/10, 2019 at 7:28 Comment(0)
P
-2

Press Ctrl - C it will stop
if not check

Pavkovic answered 30/10, 2020 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.