Capistrano and PATH


Lately, we’ve been working on getting Capistrano set up so we can have easy, predictable deployments. If you’re not using Capistrano (or some other automated deployment solution), I highly suggest looking into it. It’s a no-brainer like automated tests or source control management. However, if you’re not using it, this post won’t be of any interest to you – it’s simply a little gotcha that we ran into.
One component of Capistrano that you need to provide is a so-called spinner script. This is the script that actually starts your app. In our case, it starts a Mongrel cluster like so:
./script/process/spawner -a -p 8000 -i 3
This starts three instances of Mongrel (on ports 8000,8001, and 8002) and binds them to localhost.
When we would run Capistrano, we would see the following output:

* executing `deploy:start'
* executing "sh -c 'cd /var/www/seekler/current && nohup script/spin'"
servers: [""]

But the actual Mongrel servers would not be running (i.e. ps aux | grep mongrel showed nothing). We would ssh into the server as the Capistrano user and run the same command and everything would work fine. Huh?
The problem turned out to be that spawner calls mongrel_rails (the program that actually starts the Mongrel instances) and that program was not in the PATH when Capistrano executed this code (although it was the in PATH if we remotely logged in and ran the spin script manually. Sudo on Debian can play some weird tricks with your PATH).
Compounding the problem was that the spawner code silently fails if it can’t find mongrel_rails – which made it much harder to diagnose the issue.
We solved the problem by creating a symbolic link to mongrel_rails into /usr/local/bin
ln -s /var/lib/gems/1.8/bin/mongrel_rails /usr/local/bin
and then everything worked.
However, your PATH may be different. To find out, temporarily edit spawner.rb (the path on my machine was /var/lib/gems/1.8/gems/rails-1.2.3/lib/commands/process/spawner.rb) and add the following line at the beginning
That will print out all the environment variables as the script is run, including the PATH. Then you can create a symlink to mongrel_rails in the appropriate directory.
Hope that helps!


One Response to “Capistrano and PATH”

  1. used computers Says:

    This post was very helpful for me to look at the big picture. I often get lost in the small details and forget about the long-term plan. Thanks for the info! I’d like to recommend it with reference to my website

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: