Capistrano and Unicorn

Steve Klabnik steve at
Thu Feb 18 15:41:20 EST 2010

Anyone have any tips for using Capistrano with unicorn?

I'm following what GitHub did ( ,
and for some reason, my old master isn't dying correctly. If I `pkill
-9 unicorn_rails && unicorn_rails` the new code shows up. I'm thinking
the problem may be with how capistrano uses the 'current' directory as
a symlink.

That config's before_fork seems to make sense to me; I might just be
doing something stupid.

Here's my config, for reference:

# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 16

working_directory "/home/git/site/current" # available in 0.94.0+

# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/.sock", :backlog => 64
listen 8080, :tcp_nopush => true

timeout 600

pid "/var/run/"

# some applications/frameworks log to stderr or stdout, so prevent
# them from going to /dev/null when daemonized here:
stderr_path "/var/log/unicorn.stderr.log"
stdout_path "/var/log/unicorn.stdout.log"

# combine REE with "preload_app true" for memory savings
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and

  old_pid =  RAILS_ROOT + '/tmp/pids/'
  if File.exists?(old_pid) && != old_pid
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us


after_fork do |server, worker|

  # the following is *required* for Rails + "preload_app true",
  defined?(ActiveRecord::Base) and


I am using ree, so preload_app should be in effect.

Thanks in advance.

More information about the mongrel-unicorn mailing list