This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Having to restart Unicorn manually after each deploy would be a pain. Let's add a Capistrano task to automate this.
If we edit config/unicorn_init.sh
, we'll see APP_ROOT=/home/deploy/guestbook/current
and PID=$APP_ROOT/tmp/pids/unicorn.pid
. The process ID file, which is needed to restart the Unicorn process, is in the guestbook/current/
directory, which gets overwritten on each deploy. We need to instead store it in the shared/
directory, which is preserved between deploys.
Connect to your server via ssh
and run:
mkdir -p ~/guestbook/shared/tmp/pids/
On your development machine, in config/deploy.rb
, add:
append :linked_dirs, "tmp/pids"
From your development terminal:
bundle exec cap production deploy
Having to restart manually after each deploy is a pain, though. Let's create a task. Create lib/capistrano/tasks/deploy_restart.rake
file:
namespace :deploy do
desc "Restart Unicorn"
task :restart do
on roles(:app) do
execute "sudo /home/deploy/guestbook/current/config/unicorn_init.sh restart"
# ^^^ Depending on your environment settings you may find using
# "restart" as an argument to unicorn_init.sh doesn't reload your app's
# code. If that's the case, try using "upgrade" instead of "restart".
end
end
end
after "deploy:finishing", "deploy:restart"
If we run bundle exec cap production deploy:restart
, we'll see no tty present and no askpass program specified
. We need to edit the "sudoers" file for our deploy
user.
Connect to your server via ssh
and run:
sudo visudo -f /etc/sudoers.d/deploy
If you don't already have that file, see our App Deployment Accounts workshop to create one.
Add a line to allow unicorn_init.sh
to run without a password:
deploy ALL=(ALL:ALL) ALL
# Must be AFTER above line
deploy ALL=NOPASSWD: /home/deploy/guestbook/current/config/unicorn_init.sh
Running bundle exec cap production deploy:restart
should work now. And running bundle exec cap production deploy
should automatically restart Unicorn after the deployment.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up