170

When I am starting rails server using rails s command it is showing A server is already running. Check C:/Sites/folder/Pids/Server.pids

When I open the file it is outputting a 4 digit number only so how could I resolve this issue ?

FYI

  1. No other instance of Rails cmd is running this time.
  2. Checked Task manager but only cmd.exe is showing no else process is running. (using Windows).
Zentaurus
  • 758
  • 2
  • 11
  • 27
swapnesh
  • 26,318
  • 22
  • 94
  • 126

20 Answers20

259

TL;DR Just Run this command to Kill it

sudo kill -9 $(lsof -i :3000 -t)

Root Cause: Because PID is locked in a file and web server thinks that if that file exists then it means it is already running. Normally when a web server is closed that file is deleted, but in some cases, proper deletion doesn't happen so you have to remove the file manually New Solutions

when you run 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 /your_project_path/tmp/pids/server.pid. Exiting

So place your path shown here /your_project_path/tmp/pids/server.pid

and remove this server.pid file:

rm /your_project_path/tmp/pids/server.pid

OR Incase you're server was detached then follow below guidelines:

If you detached you rails server by using command "rails -d" then,

Remove rails detached server by using command

ps -aef | grep rails

OR by this command

sudo lsof -wni tcp:3000

then

kill -9 pID

OR use this command

To find and kill process by port name on which that program is running. For 3000 replace port on which your program is running.

sudo kill -9 $(lsof -i :3000 -t)

Old Solution:

rails s -p 4000 -P tmp/pids/server2.pid

Also you can find this post for more options Rails Update to 3.2.11 breaks running multiple servers

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
  • because PID is locked in file and web server thinks that if that file exists then it means it is already running. Normally when web server is closed that file is deleted, but in some cases proper deletion don't happen so you have to remove file manually – Taimoor Changaiz May 17 '16 at 05:17
  • Careful this will also kill your browser if it's pointing to :3000! – Aeramor Jul 21 '18 at 10:19
  • 1
    perfect solution for `sudo kill -9 $(lsof -i :3000 -t)` – new2cpp Oct 23 '19 at 07:21
  • 1
    Very nice! I created a function on my bash profile that kill the running server using this command, so you just type `kill_server` on the terminal and everything is done. I declared this on my bash profile: `kill_server() { sudo kill -9 $(lsof -i :3000 -t) }` – Victor Nov 30 '20 at 23:18
220

Remove the file: C:/Sites/folder/Pids/Server.pids

Explanation In UNIX land at least we usually track the process id (pid) in a file like server.pid. I think this is doing the same thing here. That file was probably left over from a crash.

rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • 6
    what exactly this file do and cause for this ..can you please explain it a little more :) – swapnesh Feb 25 '13 at 17:36
  • 12
    Sorry, I should have commented further. In UNIX land at least we usually track the process id (pid) in a file like server.pid. I think this is doing the same thing here. That file was probably left over from a crash. – rainkinz Feb 25 '13 at 18:18
  • I think I clone it from Git so do u think it might be a cause ? https://github.com/swapnesh/Add_twitter_bootstrap – swapnesh Feb 25 '13 at 18:22
  • 2
    After doing this I'm still getting a "Port in use" for 3000. Anyone know how I can fix this? Thanks! – daveomcd Jan 12 '14 at 01:48
  • 6
    Run `netstat -a -o` at a command prompt to see what process is using port 3000 – Rory Jan 13 '14 at 04:42
  • causes socket.rb:206:in `bind': Address already in use - bind(2) for 0.0.0.0:3000 (Errno::EADDRINUSE), check joshs answer how to resolve this after removing .pid – mmln Aug 28 '14 at 16:09
  • its `/tmp/pids` now – Phil Apr 24 '17 at 17:25
88
lsof -wni tcp:3000

Then you should see the ruby process and you can run

kill -9 processid

you should be good to run the process now

rails s thin

running multiple processes doesn't seem like a good idea and from what i've read many people agree. I've noticed many memory leaks with rails unfortunately so I couldn't imagine having two processes running. I know with one overtime my page refreshes increasingly become slower because of the data being stored on memory.

Josh Bedo
  • 3,410
  • 3
  • 21
  • 33
30
kill -9 $(lsof -i tcp:3000 -t)
Karan Purohit
  • 2,649
  • 2
  • 17
  • 10
  • 2
    @CalamityAdam Rails usually runs on port 3000. He searches for processes based on that and gets only the PID from them (`-t` option) and then kills the process by that retrieved PID. – totymedli Oct 30 '19 at 16:44
15

You can get rid of the process by killing it:

kill -9 $(lsof -i tcp:3000 -t)
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
manu
  • 199
  • 3
  • 10
12
$ lsof -wni tcp:3000

# Kill the running process
$ kill -9 5946

$ rm tmp/server.pids

foreman start etc start the service

Laurel
  • 5,965
  • 14
  • 31
  • 57
user1251378
  • 121
  • 1
  • 3
7

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
5

It happens when you kill your server process and the pid file was not updated. The best solution is to delete the file Server.pid.

Use the command

rm <path to file Server.pid>

Ashish Sharma
  • 321
  • 3
  • 13
4

On Windows Rails 5.2, delete this file

c:/Sites/<your_folder>/tmp/pids/server.pid

and run

rails s

again.

MSC
  • 3,286
  • 5
  • 29
  • 47
4

Just open that C:/Sites/folder/Pids/Server.pids and copy that 4 digit value.that 4 digit value is nothing but a PID, which you need to kill to stop the already running process.

then to stop the process use below command

 kill -9 <pid>

once that already running process get stopped then hit

rails s to start the rails server

Raju Muke
  • 41
  • 5
3

Probably you suspended the server by: ^Z.

The four digital number that vim C:/Sites/folder/Pids/Server.pidsoutputs is the process id.

You should kill -9 processid, replacing the process id with the 4 numbers that vim (or other editor) outputed.

Cherenkov
  • 485
  • 1
  • 8
  • 16
3

Run:

in Ubuntu/linux

 sudo rm /var/www/html/rails/WBPOCTEST/tmp/pids/server.pid

Or

 pkill -9 ruby

or

lsof -wni tcp:3000

kill -9 pid
Syed Shibli
  • 992
  • 1
  • 12
  • 15
3

On Ubuntu :- sudo kill -9 $(lsof -i :3000 -t)

where, 3000 is port number

Works for me....

For older versions :-

rails s -p 3000 -P tmp/pids/server2.pid

Vaibhav Jain
  • 305
  • 3
  • 8
2

If you are on Windows, you just need to do only one step as 'rails restart' and then again type 'rails s' You are good to go.

Alok Amme
  • 129
  • 1
  • 4
1

Run: fuser -k -n tcp 3000

This will kill the process running at the default port 3000.

almawhoob
  • 382
  • 3
  • 5
0

I just had this issue and tried setting it to a different port, but the only thing I needed to do was to delete my [app_directory]/tmp/pids/server.pid and everything was good to go.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
0

my docker container has no lsof. Try sudo kill -9 $(netstat -ano -p tcp | grep :3000 | awk '{ print $7 }' | grep -Po '^[\d]+')

mfittko
  • 411
  • 4
  • 3
0

I have face this issue in my rails application when i used docker in windows mechine. i just removed the server.pid file from the project and restart the rails application.

You just need to delete C:/Sites/folder/Pids/Server.pid this file

Syamlal
  • 714
  • 1
  • 11
  • 20
0

Just go to the TMP folder in your application. Go to the pids folder and delete the files present. Now rerun the server. This worked for me.

0

I was after a way I could intuitively understand (as opposed to just memorise).

All you need to know is two things

  1. Run the ps command to list processes, and
  2. Run the kill command to stop a process

Example

Then it's as simple as:

ps
# PID TTY           TIME CMD
# 32953 ttys000    0:00.71 -zsh
# 33730 ttys001    0:00.67 -zsh  
# 46844 ttys004    0:01.78 puma 4.3.12 (tcp://localhost:3000) [myapp] 

look for the PID that matches your rails server, and pass it to kill

kill -9 46844

Run ps again to see that it's now stopped.

Note

  • For more info on -9, run man kill.
stevec
  • 41,291
  • 27
  • 223
  • 311