40

In my Rails Project, I am trying to run two different servers at different port. But it fails by giving this error at console.

C:\Rails>rails s
=> Booting Mongrel
=> Rails 3.1.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
A server is already running. Check C:/Rails/tmp/pids/server.pid.Exiting

Please check the updated answer.

Rubyist
  • 6,486
  • 10
  • 51
  • 86

14 Answers14

52

After googling a lot, I just delete that file and restart the server. Then again system create that file, then again I delete that file. Now Server is running fine. And System generates another copy at the same place. But it is running well.

DELETE THAT FILE ....

If you want to run two servers then it may again create trouble. So

Both commands are checking the default PID file location (tmp/pids/server.pid), so you're seeing this error. Try running multiple servers like so:

Server 1: bundle exec rails s

Server 2: bundle exec rails s -p 3001 -P tmp/pids/server2.pid

Credit: https://stackoverflow.com/a/14446920/1376448

Thanks

UPDATE after Connor Leech comment about Forman Gem

Foreman can help manage multiple processes that your Rails app depends upon when running in development. It also provides an export command to move them into production.

Community
  • 1
  • 1
Rubyist
  • 6,486
  • 10
  • 51
  • 86
  • 1
    Try using forman gem to run your app on multiple ports. Using foreman you can declare the various processes that are needed to run your application using a Procfile http://railscasts.com/episodes/281-foreman?view=comments – Connor Leech Oct 24 '13 at 08:01
  • For what it's worth, Foreman seems to increase the amount of times I have to deal with this problem. – steel Jan 28 '15 at 17:58
  • author himself asked, himself answered after googling a lot in 2 minutes ... =) – mmike Jun 27 '16 at 08:30
  • @MikhailMorgunov :- First of all, I tried at my end. After that I posted the question and answer for all dev. I dnt think - This will harm any one. Thanks for reminding, I made the updation in question. – Rubyist Jun 27 '16 at 08:46
38

You can use netstat to know which process is holding the rails webserver, then you can kill the pid and start it over again, assuming that for some weird reason the server is not responding or running in background and you don't find another way to restart it..

netstat -plntu | grep 3000
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      7656/ruby

The last column shows the PID and the process name, then you only need to do:

kill -9 7656

and rails s to get it working again...

Hope it's useful

kainlite
  • 1,024
  • 17
  • 35
  • 2
    well in this case is not necessary but it shows udp connections and listening ports, depending on the other arguments.. – kainlite Oct 02 '13 at 20:59
  • 2
    If `netstat` doesn't work for you (http://apple.stackexchange.com/questions/97212/netstat-n-unknown-or-uninstrumented-protocol) you can still find your `pid` number in your `server.pid` file, just copy that and kill in the same way. – MCB May 28 '14 at 13:47
17

I find myself coming back to this webpage a lot to find the lsof -wni tcp:3000 command so I've found this method to be easier.

If you get this message:

A server is already running. Check /Users/username/project/tmp/pids/server.pid.
Exiting

And if you're running on a unix system (mac or linux) you can run these commands:

$ cat /Users/username/project/tmp/pids/server.pid

# output

71030

# Kill the process

$ kill -9 71030

Then run your server again!

ChrisG
  • 185
  • 2
  • 9
15

I deleted the file with cd'ing in to the tmp directory then removing the file

rm server.pid

Then I restarted the server and I got this error

Exiting/Users/josephmellin/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/socket.rb:206:in `bind': Address already in use - bind(2) for 0.0.0.0:3000 (Errno::EADDRINUSE)

Then I could use the following command to see which process is running.

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

And after I entered my password, I got this resoponse

ruby      2786 josephmellin   12u  IPv4 0xfeadd8ae849eaec9      0t0  TCP *:3000 (LISTEN)

And then killed the process with the following command

KILL -9 2786

And then restarted the server (you will have a different number than 2786 - I left it here for demo purposes)

Joe Mellin
  • 719
  • 8
  • 15
3

Step 1: remove .pid

C:/Rails/tmp/pids/server.pid.Exiting

# IN linux/unix shell
$ rm -rf <path to file>

Sometime this doesn't solve the problem, then you have to kill the process running by localhost, for such cases, follow STEP 2

STEP 2: List the process for localhost and kill it

# For Linux/Unix shell

$ lsof -wni tcp:3000

# output
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ruby    5946 rails   11u  IPv4  79073      0t0  TCP *:3000 (LISTEN)
ruby    5946 rails   12u  IPv4 103786      0t0  TCP 127.0.0.1:3000->127.0.0.1:53612 (ESTABLISHED)

# Kill the running process
$ kill -9 5946

run your server again

rails server
przbadu
  • 5,769
  • 5
  • 42
  • 67
3
ps aux | grep rails
           or 
bundle exec rails s -p 3001 -P tmp/pids/server2.pid
amn
  • 180
  • 2
  • 6
2

Every instance of a RoR server creates a PID file. By default it is

#{Rails.root}/tmp/pids/server.pid

and if that file already exists it will refuse to start a new server.

To run more than one server of the same project on the same machine you should manually specify the PID file name for each instance of the server (I recommend simply appending a hyphen and the port number) using the -P option:

rails s -p 1234 -P tmp/pids/server-1234.pid

I'm told in some cases you may need to supply a full (rather than relative) path, but I don't know what those cases are.

Old Pro
  • 24,624
  • 7
  • 58
  • 106
1

You can see the PID for each proccess(the first column) :

ps vax | grep rails
// OR:  ps auxw | grep rails

5236 pts/1    Sl+    1:46   2   0.2 /usr/bin/ruby1.9.1 script/rails s -p 3001
5298 pts/2    Sl+    0:12   2   0.7 /usr/bin/ruby1.9.1 script/rails s -p 3003
7356 pts/5    Sl+    0:09   2   0.9 /usr/bin/ruby1.9.1 script/rails s -p 3002
7846 pts/3    Sl+    0:19   2   1.7 /usr/bin/ruby1.9.1 script/rails s

Then kill the server:

kill -9 <pid>

To kill all running apps with "rails" in the name:

killall -9 rails
user2503775
  • 4,267
  • 1
  • 23
  • 41
1

Use rails default commands, for example:

rake tmp:clear

Works for me, and really simple. ;)

1

single line command, that will take care of it.

kill -9 $(more C:/Rails/tmp/pids/server.pid)
webster
  • 3,902
  • 6
  • 37
  • 59
0

I just had this problem, just deleted server.pid file and server works fine!

0

Remove that file: C:/Rails/tmp/pids/server.pid

0

A simpler way in which I found lesser commands . Go to the path which says a server is running in your folder structure . Search for the file. On the file itself shows a number which is the process id that is currently running. Lets say if the number is 'x', then simply type this command into your terminal

kill -9 x

However, note that this works in Ubuntu. Not sure, if it works in other OS as well.

-1

Try to change the number in the pid file to another and save it.

xwyy
  • 31
  • 4