104
..$ rails s
=> Booting WEBrick
=> Rails 4.0.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
A server is already running. Check /home/..name/rprojects/railsapp/tmp/pids/server.pid.
Exiting

what is the easiest way to solve this for a rails beginner?

ben
  • 6,000
  • 5
  • 35
  • 42

20 Answers20

213

You can delete the server.pid file.

rm /your_project_path/tmp/pids/server.pid

Else:

try in OSX:

sudo lsof -iTCP -sTCP:LISTEN -P | grep :3000

or in linux:

ps -aef | grep rails

or

lsof -wni tcp:3000

kill the process using

kill -9 PID (eg,2786)
ben
  • 6,000
  • 5
  • 35
  • 42
151

Short and Crisp single line command, that will take care of it.

kill -9 $(lsof -i tcp:3000 -t)
Karan Purohit
  • 2,649
  • 2
  • 17
  • 10
  • 8
    lol, may want to close your browser tab first if you have it open to localhost:3000, otherwise this may kill your browser – TMin Nov 18 '16 at 20:11
  • anyone tried saving this as an alias in zsh and it not working? – stackjlei Jun 22 '17 at 23:25
  • Sometimes my server will have been shut down but ruby has maxed out CPU. When this is the case I have to kill `ruby` by running `top` getting the PID and then running `kill -9 ` – daveomcd Sep 18 '18 at 16:22
22

server.pid only contains the process ID of the running server.

If you do:

more /your_project_path/tmp/pids/server.pid

you will get a number (say 6745) which you can use to stop the previous server with the command kill:

kill -9 6745

and then you can remove the file with the rm command

rm /your_project_path/tmp/pids/server.pid
kangkyu
  • 5,252
  • 3
  • 34
  • 36
muichkine
  • 2,890
  • 2
  • 26
  • 36
18

If you are using docker-compose, and in docker-compose.yml have: volumes: - .:/myapp That means you local workspace is mapped to the container's /myapp folder.

Anything in /myapp will not be deleted for the volumes define.

You can delete ./tmp/pids/server.pid in you local machine. Then the container's /myapp will not have this file.

Lane
  • 4,682
  • 1
  • 36
  • 20
  • 6
    You could also simply delete that file automatically on every container start: `command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"`. Refs: https://github.com/docker/compose/issues/1393#issuecomment-99988242 – leymannx Mar 18 '18 at 21:42
  • Only this has worked for me to remove server.pid from my local machine, not from the container or image. – Sanjay Prajapati Jan 16 '21 at 16:14
  • A very slick solution, that avoids deleting the pid every time the compose file is run, is documented at https://ieftimov.com/posts/docker-compose-stray-pids-rails-beyond/ – David Baucum Sep 20 '22 at 14:36
14

Simple:

go in the root folder of the project when this happens and run:

gem install shutup
shutup

This will find the process currently running, kill it and clean up the pid file

NOTE: if you are using rvm install the gem globally

rvm @global do gem install shutup
Lorenzo Sinisi
  • 450
  • 5
  • 8
14

Run this command -

lsof -wni tcp:3000

then you will get the following table -

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ruby    2552 shyam   17u  IPv4  44599      0t0  TCP 127.0.0.1:3000 (LISTEN)
ruby    2552 shyam   18u  IPv6  44600      0t0  TCP [::1]:3000 (LISTEN)

Run this command and replace PID from the Above table

kill -9 PID

example:

kill -9 2552
Pradnyesh patil
  • 271
  • 3
  • 3
5

Issue can be solved using:

kill -9 $(more /home/..name/rprojects/railsapp/tmp/pids/server.pid)
webster
  • 3,902
  • 6
  • 37
  • 59
5

It happens sometimes because you turn off the server by force, for example turning off the OS/machine manually so that the server does not have enough time to log to server.pid.

One easy way is to manually go to tmp/pids/ (the directory that is shown in your console.) and remove server.pid file. Then, when you start the server again, rails server or rails s ,it creates a new server.pid and you can continue development.

DragonKnight
  • 1,740
  • 2
  • 22
  • 35
5

First Find PID # - where the Rails Server got stuck on

Run this to find the stuck PID

cat ./tmp/pids/server.pid

It will return something like 65829

Then KILL that PID => kill 65829

Jonathan Sanchez
  • 7,316
  • 1
  • 24
  • 20
4

Kill server.pid by using command:

kill -9 `cat /root/myapp/tmp/pids/server.pid`

Note: Use your server.pid path which display in console/terminal.

Thank you.

Rahul2692
  • 334
  • 1
  • 10
3

the gui way for Windows user

open the ResourceMonitor (taskmanager ->Performance -> ResourceMonitor) and kill the ruby.exe process

enter image description here

Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35
3

For added information, under the context of running the application in docker.

In docker-compose.yml file, under the application container itself, you can use one of the following:

command: ["rm /your-app-path/tmp/pids/server.pid && bundle exec bin/rails s -p 3000 -b '0.0.0.0'"]

or

command: ["rm /your-app-path/tmp/pids/server.pid; foreman start"]

Note the use of either ; or &&, that && will send an exit signal if rm fails to find the file, forcing your container to prematurely stop. Using ; will continue to execute.

Why is this caused in the first place? The rationale is that if the server (puma/thin/whatever) does not cleanly exit, it will leave a pid in the host machine causing an exit error.

For portability rather than manually deleting the file on the host system, it's better to check if the file exists within scripted or compose file itself.

rnevius
  • 26,578
  • 10
  • 58
  • 86
mirageglobe
  • 2,446
  • 2
  • 24
  • 30
2

Open the path/to/your/rails/project/tmp/pids/server.pid file.

Copy the number you find therein.

Run kill -9 [PID]

Where [PID] is the number you copied from the server.pid file.

This will kill the running server process and you can start your server again without any trouble.

0

For this problem,

What i did is:

  • Delete the Pids folder which is located under app/tmp/
  • and then, close the terminal which we are running the current app and close the tab (in, browser window)

  • after that, again open the terminal by going inside the folder, and then do, rails s

  • Then, it opens the fresh tab which is running our application
Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
sowmya
  • 51
  • 2
  • 8
0

first copy the cumber inside the file then remove it: rm /your_project_path/tmp/pids/server.pid then create it again. touch /YOUR_PROJECT_PATH/tmp/pids/server.pid It worked for me.

0

Run given below command on terminal ( for linux only )

ps aux | grep rails

and then

kill -9 [pid]

Another way

lsof -wni tcp:3000

and then

kill -9 [PID]
Arvind singh
  • 1,312
  • 15
  • 15
0

SOLVING

Address already in use — bind(2)” 500 error in Ruby on Rails

Recently I tried running a Rails app on a production server. Not only did it not work, but it broke my localhost:3000 development server as well. Localhost would only load a blank white page or a 500 error.

To solve this, I used two quick commands. If these don’t return a result, you may need to look elsewhere for a solution, but this is a good quick fix.

lsof -wni tcp:3000


ruby    52179 rachelchervin   50u  IPv6 0x...7aa3      0t0  TCP [::1]:hbci (LISTEN)
ruby    52179 rachelchervin   51u  IPv4 0x...c7bb      0t0  TCP 127.0.0.1:hbci (LISTEN)
ruby    52180 rachelchervin   50u  IPv6 0x...7aa3      0t0  TCP [::1]:hbci (LISTEN)
ruby    52180 rachelchervin   51u  IPv4 0x...c7bb      0t0  TCP 127.0.0.1:hbci (LISTEN)

This command shows all of my currently running processes and their PIDs (process IDs) on the 3000 port. Because there are existing running processes that did not close correctly, my new :3000 server can’t start, hence the 500 error.

kill 52179

kill 52180

rails s

I used the Linux kill command to manually stop the offending processes. If you have more than 4, simply use kill on any PIDs until the first command comes back blank. Then, try restarting your localhost:3000 server again. This will not damage your computer! It simply kills existing ruby processes on your localhost port. A new server will start these processes all over again. Good luck!

Thomas
  • 1
  • 2
0

SOLUTION for Windows:

  1. see the pid from \tmp\pids\server.pid
  2. open cmd as administrator
  3. taskkill /F /PID [pid from step 1.]
  4. delete server.pid file
  5. restart server
0

For ubuntu 20, kill -9 $(ps -aef | grep rails)

riddhi
  • 146
  • 5
  • 1
    Trying to follow the code. Where does the value of 9 come from? I get invalid argument. – Michael Dec 23 '21 at 19:57
  • This is the basic command to kill a process: kill [options] [process_ids]. 9 is one of the options that comes with the kill command. It stands for SIGKILL( Singal Kill), with SIGKILL we ask the kernel to immediately shut down the process. The process dies and won’t clean up after itself. – riddhi Dec 25 '21 at 01:45
  • You can find a process id (pid) by the port number on which it was running(say 3000) and kill it with this command: sudo kill -9 $(sudo lsof -t -i:3000) – riddhi Dec 25 '21 at 01:54
0

Just delete server.pid file, in my case rm ./tmp/pids/server.pid . You can also delete it manually

Kip
  • 11
  • 1