Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fully restart Ruby on Rails app under Phusion Passenger during Capistrano (cap) deploy
# Most instructions for using Capistrano tell you how to make it restart Phusion
# Passenger by 'touch'ing the restart.txt file, but this doesn't immediately
# restart the app - instead the first person to try to use the application will
# cause it to be restarted, so they will see a delay of at least a few seconds.
# This shows how to add a post-deploy task to 'ping' the server, to cause it to
# restart immediately.
# First, (optionally) add a '/ping' route to config/routes.rb that immediately
# returns a blank page. This prevents any non-essential work being done, such as
# rendering the homepage. Test it to make sure you get a blank page not an
# error.
# Then add a deploy:ping task to config/deploy.rb and set it to run
# automatically after deploy:restart. Alternatively you could put it into the
# deploy:restart task directly.
# config/deploy.rb:
# Add this to the settings section at the top:
set :ping_url, ""
# This is the standard Phusion Passenger restart code. You will probably already
# have something like this (if you have already got Capistrano set up).
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, roles: :app, except: { no_release: true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
# Add this to add the deploy:ping task:
namespace :deploy do
task :ping do
system "curl --silent #{fetch(:ping_url)}"
# Add this to automatically ping the server after a restart:
after "deploy:restart", "deploy:ping"
# config/routes.rb:
SampleApp::Application.routes.draw do
# ...
# Add this line somewhere in the file:
# Note: If you have a catch-all route then this line must come before it
get 'ping' => proc {|env| [200, {}, []] }
# Alternatively you could output a short string to confirm it worked
# instead of showing a blank page
#get 'ping' => proc {|env| [200, {}, ['pong']] }

This comment has been minimized.

Copy link

tdwalton commented Aug 8, 2013

Thanks for this. I was looking for a work-around to the delay caused by a restart.

While looking for a way to make it work in a multi-server environment, I ran across this stack overflow question.

It might help others who happen across this gist.

I suppose you could just as easily use, but this method allows you to restrict it to only the server roles you want.


This comment has been minimized.

Copy link

shredding commented Nov 5, 2013



This comment has been minimized.

Copy link

wesgarrison commented Aug 15, 2014

system didn't work for me, so I had to use execute, making the new command:

execute :curl, ' --silent', fetch(:ping_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.