[Rails] Make capistrano ask for the password first

I frequently find myself doing rake deploy with capistrano, and then wander off to do something else, while the checkout process runs its minute-long course. And then I forget what I was doing, and only 15 minutes later stumble upon that shell buffer again, only to discover that it’s still sitting there, waiting for my sudo password so it can restart the dispatchers.



Well, with this little snippet in my deploy.rb, it now asks for my sudo password up-front, so it’ll still be cached and valid when it really needs it at the end:



task :before_deploy, :roles => :app do
  send(run_method, "date")
end


That’s it: Force it to execute some shell command using sudo. It doesn’t matter which, so long as it doesn’t actually do anything. I chose date. Calling run_method instead of sudo directly will ensure that if you’re not using sudo, it won’t ask for your password here, either.

4 comments

Sean Treadway

Brilliant. I actually just ran into the same problem today.
Read more
Read less
  Cancel
I'm glad it could be of use. Rock on.
Read more
Read less
  Cancel
You can also just pass "-p" to your capistrano call. Like "cap deploy -p". That'll force the question up front.
Read more
Read less
  Cancel
Haha, thanks, David, I was always using the "rake" version directly, so I never noticed.
Read more
Read less
  Cancel

Leave a comment