Search code examples
ruby-on-railsunicorn

Can't start unicorn properly with error "already running"


I want deploy a rails project to Ubuntu 14.04 server by using capistrano and let it run with unicorn.

I created a unicorn:start task like this:

namespace :unicorn do
  task :environment do
    set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
    set :unicorn_config, "#{current_path}/config/unicorn/#{fetch(:rails_env)}.rb"
  end
  def start_unicorn
    within current_path do
      execute :bundle, :exec, :unicorn, "-c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -D"
    end
  end

  desc "Start unicorn server"
  task :start => :environment do
    on roles(:app) do
      start_unicorn
    end
  end
end

And this is the result of bundle exec cap production unicorn:start

DEBUG [525e1e88] Running /usr/bin/env [ -d ~/.rbenv/versions/2.2.3 ] as [email protected]
DEBUG [525e1e88] Command: [ -d ~/.rbenv/versions/2.2.3 ]
DEBUG [525e1e88] Finished in 0.465 seconds with exit status 0 (successful).
DEBUG [6dc9e26a] Running /usr/bin/env if test ! -d /var/www/us.example.com/current; then echo "Directory does not exist '/var/www/us.example.com/current'" 1>&2; false; fi as [email protected]
DEBUG [6dc9e26a] Command: if test ! -d /var/www/us.example.com/current; then echo "Directory does not exist '/var/www/us.example.com/current'" 1>&2; false; fi
DEBUG [6dc9e26a] Finished in 0.052 seconds with exit status 0 (successful).
DEBUG [81c26179] Running /usr/bin/env [ -e /var/www/us.example.com/shared/tmp/pids/unicorn.pid ] && kill -0 `cat /var/www/us.example.com/shared/tmp/pids/unicorn.pid` as [email protected]
DEBUG [81c26179] Command: [ -e /var/www/us.example.com/shared/tmp/pids/unicorn.pid ] && kill -0 `cat /var/www/us.example.com/shared/tmp/pids/unicorn.pid`
DEBUG [81c26179] Finished in 0.053 seconds with exit status 1 (failed).
INFO [a5655cfe] Running RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 ~/.rbenv/bin/rbenv exec bundle exec unicorn -c /var/www/us.example.com/current/config/unicorn/production.rb -E deployment -D  as [email protected]
DEBUG [a5655cfe] Command: cd /var/www/us.example.com/current && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 RAILS_ENV=production RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 ~/.rbenv/bin/rbenv exec bundle exec unicorn -c /var/www/us.example.com/current/config/unicorn/production.rb -E deployment -D  )
INFO [a5655cfe] Finished in 3.770 seconds with exit status 0 (successful).
DEBUG [dd3f31fa] Running /usr/bin/env if test ! -d /var/www/us.example.com/current; then echo "Directory does not exist '/var/www/us.example.com/current'" 1>&2; false; fi as [email protected]
DEBUG [dd3f31fa] Command: if test ! -d /var/www/us.example.com/current; then echo "Directory does not exist '/var/www/us.example.com/current'" 1>&2; false; fi
DEBUG [dd3f31fa] Finished in 0.055 seconds with exit status 0 (successful).
INFO [1140efb6] Running RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 ~/.rbenv/bin/rbenv exec bundle exec unicorn -c /var/www/us.example.com/current/config/unicorn/production.rb -E production -D as [email protected]
DEBUG [1140efb6] Command: cd /var/www/us.example.com/current && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 RBENV_ROOT=~/.rbenv RBENV_VERSION=2.2.3 ~/.rbenv/bin/rbenv exec bundle exec unicorn -c /var/www/us.example.com/current/config/unicorn/production.rb -E production -D )
DEBUG [1140efb6]    master failed to start, check stderr log for details

And in the log shows pid=': Already running on PID:10760.

Unicorn itself is executed, but somehow the start process runs twice, so second running process fails. I could find that, but I can't find out why the running process runs two times.

What should I check to solve this problem?


Solution

  • Sorry a bit off-topic. Have you try to use existing 3rd party plugin instead of writing your own Capistrano task? You can find them at:

    http://capistranorb.com/documentation/third-party-plugins/

    https://github.com/tablexi/capistrano3-unicorn