title: Unicorn PID is stale on initial deploy
body: <p>I'm trying to use capistrano and unicorn for the first time in my rails application. I can deploy successfully except unicorn won't start with the following error from stderr:</p>
  
  <pre><code>I, [2013-03-25T16:55:35.877323 #2378]  INFO -- : reloading config_file=/var/www/good/current/config/unicorn.rb E, [2013-03-25T16:55:35.881496 #2378] ERROR -- : error reloading config_file=/var/www/good/current/config/unicorn.rb: Already running on PID:2378 (or pid=/var/www/good/current/tmp/pids/unicorn.pid is stale) (ArgumentError) E, [2013-03-25T16:55:35.881548 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:193:in `pid=' E, [2013-03-25T16:55:35.881579 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:111:in `block in commit!' E, [2013-03-25T16:55:35.881606 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:108:in `each' E, [2013-03-25T16:55:35.881645 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:108:in `commit!' E, [2013-03-25T16:55:35.881674 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:699:in `load_config!' E, [2013-03-25T16:55:35.881703 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:308:in `join' E, [2013-03-25T16:55:35.881731 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/bin/unicorn:126:in `&lt;top (required)&gt;' E, [2013-03-25T16:55:35.881758 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `load' E, [2013-03-25T16:55:35.881786 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `&lt;main&gt;' I, [2013-03-25T16:55:36.233632 #2378]  INFO -- : reaped
  #&lt;Process::Status: pid 2392 exit 0&gt; worker=1 I, [2013-03-25T16:55:36.234045 #2378]  INFO -- : reaped
  #&lt;Process::Status: pid 2397 exit 0&gt; worker=3 I, [2013-03-25T16:55:36.234560 #2378]  INFO -- : reaped
  #&lt;Process::Status: pid 2394 exit 0&gt; worker=2 I, [2013-03-25T16:55:36.336246 #8587]  INFO -- : executing ["/var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn", "-c", "/var/www/good/current/config/unicorn.rb", "-E", "production", "-D", {12=&gt;#&lt;Kgio::UNIXServer:fd 12&gt;, 13=&gt;#&lt;Kgio::TCPServer:fd 13&gt;}] (in /var/www/good/releases/20130325165445) I, [2013-03-25T16:55:36.336510
  #8587]  INFO -- : forked child re-executing... I, [2013-03-25T16:55:38.475972 #2378]  INFO -- : reaped
  #&lt;Process::Status: pid 2389 exit 0&gt; worker=0 I, [2013-03-25T16:55:38.476109 #2378]  INFO -- : master complete
  </code></pre>
  
  <p>the key being</p>
  
  <blockquote>
    <p>error reloading config_file=/var/www/good/current/config/unicorn.rb: Already running on PID:2378</p>
  </blockquote>
  
  <p>I've included Gemfile, deploy.rb and unicorn.rb</p>
  
  <p>I'd appreciate any help or thoughts. I'm also using RVM and ubuntu</p>
  
  <p><strong>config/deploy.rb</strong></p>
  
  <pre><code>require "rvm/capistrano"
  set :rvm_type, :system
  set :rvm_install_with_sudo, true
  
  require "bundler/capistrano"
  set :bundle_without, [:darwin, :development, :test]
  
  set :application, "Talking GOOD"
  set :repository,  "git@github.com:FTW-Development/good.git"
  set :scm_user, "hunter@ftwdev.com"
  ssh_options[:forward_agent] = true
  
  
  #set :migrate_target, :current
  #set :ssh_options, { :forward_agent =&gt; true }
  
  #set :rails_env, "production"
  set :deploy_to, "/var/www/good"
  #set :normalize_asset_timestamps, false #what?
  set :user, "rails"
  set :group, "www"
  set :use_sudo, false
  
  
  
  
  set :keep_releases, 5
  after "deploy:restart", "deploy:cleanup"
  
  require 'capistrano-unicorn'
  #after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded
  after 'deploy:restart', 'unicorn:restart'  # app preloaded
  
  
  
  #role :web, "your web-server here"                          # Your HTTP server, Apache/etc
  #role :app, "your app-server here"                          # This may be the same as your `Web` server
  #role :db,  "your primary db-server here", :primary =&gt; true # This is where Rails migrations will run
  #role :db,  "your slave db-server here"
  
  
  
  #set :stages, %(production, staging)
  #set :default_stage, "staging"
  #require 'capistrano/ext/multistage'
  
  #set(:unicorn_env) { rails_env }
  #set(:app_env)     { rails_env }
  #role(:web) { domain }
  #role(:app) { domain }
  #role(:db, :primary =&gt; true) { domain }
  
  #set(:deploy_to)    { "/home/#{user}/#{application}/#{fetch :app_env}" }
  #set(:current_path) { File.join(deploy_to, current_dir) }
  
  server "good.ftwdev.com", :app, :web, :db, :primary =&gt; true
  </code></pre>
  
  <p><strong>config/unicorn.rb</strong></p>
  
  <pre><code>app_path = "/var/www/good/current"
  
  # Set unicorn options
  worker_processes 1
  preload_app true
  timeout 180
  listen "127.0.0.1:9000"
  
  # Spawn unicorn master worker for user apps (group: apps)
  user 'rails.www' 
  
  # Fill path to your app
  working_directory "/var/www/good/current"
  
  # Should be 'production' by default, otherwise use other env 
  rails_env = ENV['RAILS_ENV'] || 'production'
  
  # Log everything to one file
  stderr_path "log/unicorn.log"
  stdout_path "log/unicorn.log"
  
  # Set master PID location
  pid "#{app_path}/tmp/pids/unicorn.pid"
  
  before_fork do |server, worker|
    ActiveRecord::Base.connection.disconnect!
  
    old_pid = "#{server.config[:pid]}.oldbin"
    if File.exists?(old_pid) &amp;&amp; server.pid != old_pid
      begin
        Process.kill("QUIT", File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
        # someone else did our job for us
      end
    end
  end
  
  after_fork do |server, worker|
    ActiveRecord::Base.establish_connection
  end
  </code></pre>
  
  <p><strong>Gemfile</strong></p>
  
  <pre><code>source 'https://rubygems.org'
  
  gem 'rails', '3.2.8'
  
  # Bundle edge Rails instead:
  # gem 'rails', :git =&gt; 'git://github.com/rails/rails.git'
  
  gem 'mysql2'
  
  gem 'devise', '~&gt; 2.1.0'
  gem 'cancan'
  
  # Gems used only for assets and not required
  # in production environments by default.
  group :assets do
    gem 'sass-rails',   '~&gt; 3.2.3'
    gem 'coffee-rails', '~&gt; 3.2.1'
    gem 'bootstrap-sass', '~&gt; 2.2.2.0'
  
    # See https://github.com/sstephenson/execjs#readme for more supported runtimes
    # gem 'therubyracer', :platforms =&gt; :ruby
  
    gem 'uglifier', '&gt;= 1.0.3'
  end
  
  
  gem 'jquery-rails'
  gem 'haml'
  gem 'hpricot'
  gem 'ruby_parser'
  gem 'simple_form'
  gem 'high_voltage'
  gem 'paperclip', '~&gt; 3.0'
  gem 'twitter-bootstrap-rails'
  
  
  group :production do
    gem 'rb-inotify', '~&gt; 0.9'
    gem 'execjs'
    gem 'therubyracer'
    gem 'unicorn', '~&gt; 4.6'
  end
  
  group :development do
    gem 'thin'
    gem 'growl'
    gem 'guard'
    gem 'guard-bundler'
    gem 'guard-livereload'
    gem 'guard-rails'
    gem 'guard-rspec'
    gem 'rack-livereload'
    gem 'html2haml'
    gem 'capistrano'
    gem 'capistrano-unicorn'
    gem 'rvm-capistrano'
    gem 'rb-fsevent', :require =&gt; false
    gem 'rb-inotify', '~&gt; 0.9', :require =&gt; false
    gem 'rb-fchange', :require =&gt; false
  end
  
  
  #gem "rspec-rails", "&gt;= 2.12.1", :group =&gt; [:development, :test]
  #gem "database_cleaner", "&gt;= 0.9.1", :group =&gt; :test
  #gem "email_spec", "&gt;= 1.4.0", :group =&gt; :test
  #gem "cucumber-rails", "&gt;= 1.3.0", :group =&gt; :test, :require =&gt; false
  #gem "launchy", "&gt;= 2.1.2", :group =&gt; :test
  #gem "capybara", "&gt;= 2.0.2", :group =&gt; :test
  #gem "factory_girl_rails", "&gt;= 4.1.0", :group =&gt; [:development, :test]
  #gem "bootstrap-sass", "&gt;= 2.1.1.0"
  #gem "devise", "&gt;= 2.2.0"
  #gem "cancan", "&gt;= 1.6.8"
  #gem "rolify", "&gt;= 3.2.0"
  #gem "simple_form", "&gt;= 2.0.4"
  #gem "quiet_assets", "&gt;= 1.0.1", :group =&gt; :development
  #gem "figaro", "&gt;= 0.5.2"  #used for configurations
  #gem "better_errors", "&gt;= 0.2.0", :group =&gt; :development
  #gem "binding_of_caller", "&gt;= 0.6.8", :group =&gt; :development
  
  
  
  
  # To use ActiveModel has_secure_password
  # gem 'bcrypt-ruby', '~&gt; 3.0.0'
  
  # To use Jbuilder templates for JSON
  # gem 'jbuilder'
  
  # Use unicorn as the app server
  # gem 'unicorn'
  
  # Deploy with Capistrano
  # gem 'capistrano'
  
  # To use debugger
  # gem 'debugger'
  </code></pre>