Fixing "A server is already running" with Ruby on Rails

You just tried to spin up your rails server but it failed and threw an error message...

A server is already running Check /tmp/pids/server.pid."

So why did it fail and what does this mean?


How is the Server Already Running?

Well, most of the time it's not actually running.

Although you can check if it is by visiting your localhost:3000 endpoint locally.

What this error is telling us is that based on the existing PID file, Rails thinks it's already running when often it is not.

How to Learn AWS
How to Learn AWS
Learn AWS the easy way, become a leader on your team.
How to Learn AWS
LEARN MORE

Whenever you start the server, Ruby on Rails uses a strategy that involves saving the process identified (known as the PID) to a file. If you start the server again, it will first check to see if this file exists already, assuming that means a server was already started, and if it does exist it throws the error "A server is already running Check /tmp/pids/server.pid.".

When the Rails server is closed under normal circumstances, this PID file is automatically deleted.

But sometimes that process can be interrupted and the PID file isn't deleted properly.

This leaves us having to resolve and delete the PID file manually.


How to Delete the PID File

There are 2 approaches to clearing out an old PID file.

Kill the Process Through the Command Line

On OSX

If you're on OSX, we can look for any processes using port 3000 which would represent our Rails server.

lsof -wni tcp:3000

This will return the PID of any process running on port 3000.

We can pass that PID to the following command to end the process.

kill -9 <PID here>

And if you want to create a macro or save this as a one-liner, both commands can be combined like so:

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

On Linux

We can look for currently running processes named rails with the below command:

ps -aef | grep rails

Additionally you can search by port and find processes running on port 3000.

lsof -wni tcp:3000

With either approach you can take the PID identified and kill the process with that PID.

kill -9 <PID here>

Manually Deleting Your PID File

You may not be able to identify the PID through the command line steps above.

If this is the case, we can most likely safely delete this file manually.

If you are in your project's directory, navigate to and delete this file:

tmp/pids/server.pid

That should resolve the issue and allow you to run your server again!


Wrapping Up

I was running into this error consistently because I would turn off my computer without properly shutting down the server. When I booted it back up, I realized Rails wasn't able to properly delete the PID file and I would have to manually clean this up every time.

Luckily there aren't a lot of steps and it's a quick and easy fix.

Hope this helped!

Featured
Level up faster
Hey, I'm Nick Dill.

I help people become better software developers with daily tips, tricks, and advice.
Related Articles
More like this
How to Export to CSV with Ruby on Rails
Adding Active Storage to your Rails Project
What is MVC and Why Should I Use It?