Kill Rails server : a how-to guide

Posted May 2, 2022 - tagged ruby-on-rails

4 min read

How to kill your Rails server ? This could happen if your Rails server is buggy, and restart is not possible.

Short answer

If you local Rails server is running on port 3000, you have first to discover what PID is used :

$> lsof -wni tcp:3000
COMMAND   PID  USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
ruby    51195 shino   10u  IPv4 0x3cd31f222cb761f      0t0  TCP 127.0.0.1:hbci (LISTEN)
ruby    51195 shino   11u  IPv6 0x3cd31f215db234f      0t0  TCP [::1]:hbci (LISTEN)

The column that matters is "PID". The relevant number is 51195 here. On your computer it will probably be another number, but for our example, the solution will be

kill -9 51195

Replace 51195 by your own PID and that's it.

Alternative : use the shutup gem

If this is a problem that happends too frequently, you can install a gem dedicated to this problem : shutup. Repository and documentation available here

Tutorial from scratch

If you want to test the problem "from scratch", here is a small tutorial. Ruby 3 and Rails 7 will be used.

Prerequisites

$> ruby -v  
ruby 3.0.0p0 // you need at least version 3 here  
$> bundle -v  
Bundler version 2.2.11  
$> npm -v  
8.3.0 // you need at least version 7.1 here  
$> yarn -v  
1.22.10

Install fresh new Rails app

$> mkdir kill_rails && cd kill_rails  
$/kill_rails> echo "source 'https://rubygems.org'" > Gemfile  
$/kill_rails> echo "gem 'rails', '7.0.0'" >> Gemfile  
$/kill_rails> bundle install  
$/kill_rails> bundle exec rails new . --force
$/kill_rails> bundle update  
$/kill_rails> bin/rails db:create  
$/kill_rails> bin/rails db:migrate  

Check everything is working by running

$/kill_rails> bin/rails server
=> Booting Puma
=> Rails 7.0.0 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.0-p0) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 88968
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

Side note The PID is actually displayed once in the verbose logging that appears right after you started the server. See above.

Server should run locally without error, and the default Rails page application should appear at http://localhost:3000

Now stop your local server.

Open the Gemfile and add

gem "shutup", group: :development

Then

$/kill_rails> bundle install

Check that the Rails server runs properly

Then restart your local Rails app

Then

$/kill_rails> bin/rails server
=> Booting Puma
=> Rails 7.0.0 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.0-p0) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 89102
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

Notice that PID has changed : now the value is 89102.

Leave your terminal window "as-is", with the local web server running.

Kill the local Rails web server

Open another terminal window.

Given the PID we just noticed, and the 1st paragraph of this tutorial, we can type :

$> lsof -wni tcp:3000
COMMAND   PID  USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
ruby    89102 shino   10u  IPv4 0x3cd31f229b0f9df      0t0  TCP 127.0.0.1:hbci (LISTEN)
ruby    89102 shino   11u  IPv6 0x3cd31f215db1d2f      0t0  TCP [::1]:hbci (LISTEN)

You can notice the PID is 89102, as expected

Type

kill -9 89102

And go back to the first terminal window. You should see something like this :

Killed: 9
$/kill_rails>

The last instruction shows that the local Rails server was actually killed.

Kill the Rails server again, using the gem

$/kill_rails>  bin/rails server
=> Booting Puma
=> Rails 7.0.0 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.0-p0) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 90704
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

The PID value is now 90704.

Open another terminal window, and type

$kill_rails> shutup
Killed process id: 90704
$kill_rails>

Ok ! what just happened is clear enough :)

Go back to the terminal window where you server was running :

Killed: 9
$/kill_rails>

Same result as above, hopefully.

Enjoy !

Build your MVP with Rails

👉 We are 2 Ruby-on-Rails lovers. By building BootrAils, we hope we will help you to launch your Startup idea (really) faster.
Take care ! Health first. 🙏
Shino & David.

🎉 Special offer : 10% off for lifetime license
Download