我的服务器上安装了两个Rails4-Apps,Capistrano 2.15,Unicorn和Nginx。
当我cap:deploy
时,我收到此错误:
executing "/etc/init.d/unicorn_myapp1 restart"
servers: ["123.45.678.910"]
[123.45.678.910] executing command
** [out :: 123.45.678.910] Couldn't reload, starting 'cd/home/deployer/apps/myapp1/current; bundle exec unicorn -D -c /home/deployer/apps/myapp1/current/config/unicorn.rb -E production' instead
** [out :: 123.45.678.910]
** [out :: 123.45.678.910] master failed to start, check stderr log for details
command finished in 1845ms
failed: "rvm_path=$HOME/.rvm $HOME/.rvm/bin/rvm-shell 'ruby-2.0.0-p353' -c '/etc/init.d/unicorn_myapp1 restart'" on 123.45.678.910
更多信息:
如果我在服务器上手动终止工作程序,则部署过程 工作(但重启错误仍然显示)。
我怀疑,错误与事实有关,我正在同时运行2个应用程序。如果我只运行一个应用程序就行了。
finalize_update
步骤的输出:
* 2014-01-13 13:31:11 executing `deploy:finalize_update'
triggering before callbacks for `deploy:finalize_update'
* 2014-01-13 13:31:11 executing `deploy:assets:symlink'
* executing "rm -rf /home/deployer/apps/myapp1/releases/20140113123110/public/assets && mkdir -p /home/deployer/apps/myapp1/releases/20140113123110/public && mkdir -p /home/deployer/apps/myapp1/shared/assets && ln -s /home/deployer/apps/myapp1/shared/assets /home/deployer/apps/myapp1/releases/20140113123110/public/assets"
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
command finished in 816ms
* 2014-01-13 13:31:12 executing `bundle:install'
* executing "cd /home/deployer/apps/myapp1/releases/20140113123110 && bundle install --gemfile /home/deployer/apps/myapp1/releases/20140113123110/Gemfile --path /home/deployer/apps/myapp1/shared/bundle --deployment --quiet --without development test"
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
command finished in 2048ms
* executing "chmod -R -- g+w /home/deployer/apps/myapp1/releases/20140113123110 && rm -rf -- /home/deployer/apps/myapp1/releases/20140113123110/public/system && mkdir -p -- /home/deployer/apps/myapp1/releases/20140113123110/public/ && ln -s -- /home/deployer/apps/myapp1/shared/system /home/deployer/apps/myapp1/releases/20140113123110/public/system && rm -rf -- /home/deployer/apps/myapp1/releases/20140113123110/log && ln -s -- /home/deployer/apps/myapp1/shared/log /home/deployer/apps/myapp1/releases/20140113123110/log && rm -rf -- /home/deployer/apps/myapp1/releases/20140113123110/tmp/pids && mkdir -p -- /home/deployer/apps/myapp1/releases/20140113123110/tmp/ && ln -s -- /home/deployer/apps/myapp1/shared/pids /home/deployer/apps/myapp1/releases/20140113123110/tmp/pids"
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
command finished in 512ms
triggering after callbacks for `deploy:finalize_update'
* 2014-01-13 13:31:15 executing `deploy:symlink_config'
* executing "ln -nfs /home/deployer/apps/myapp1/shared/config/database.yml /home/deployer/apps/myapp1/releases/20140113123110/config/database.yml"
servers: ["xxx.xxx.xxx.xxx"]
[xxx.xxx.xxx.xxx] executing command
command finished in 615ms
triggering after callbacks for `deploy:update_code'
* 2014-01-13 13:31:15 executing `deploy:assets:precompile'
Nginx.conf :
upstream myapp.example.com {
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 80;
server_name myapp.example.com;
root /home/deployer/apps/myapp/current/public;
location ~ ^/(assets)/ {
root /home/deployer/apps/myapp/current/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @myapp.example.com;
location @myapp.example.com {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://myapp.example.com;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
独角兽日志显示此错误:
/home/deployer/apps/myapp1/shared/bundle/ruby/2.0.0/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:219:in `pid=': Already running on PID:20755 (or $
from /home/deployer/apps/myapp1/shared/bundle/ruby/2.0.0/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:142:in `start'
from /home/deployer/apps/myapp1/shared/bundle/ruby/2.0.0/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
from /home/deployer/apps/myapp1/shared/bundle/ruby/2.0.0/bin/unicorn:23:in `load'
from /home/deployer/apps/myapp1/shared/bundle/ruby/2.0.0/bin/unicorn:23:in `<main>'
这是我的 unicorn.rb 文件:
root = "/home/deployer/apps/myapp1/current"
working_directory root
pid "#{root}/tmp/pids/unicorn-myapp1.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.myapp1.sock"
worker_processes 2
timeout 30
# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end
Capfile :
load 'deploy'
load 'deploy/assets'
load 'config/deploy'
这是我的 unicorn_init.sh :
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/myapp1/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
这是我的 deploy.rb :
require "bundler/capistrano"
require "rvm/capistrano"
set :rvm_ruby_string, 'ruby-2.0.0-p353'
set :rvm_type, :user
server "123.456.789.101", :web, :app, :db, primary: true
set :application, "myapp1"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git@github.com:myusername/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
before "deploy:restart", :symlink_directories
task :symlink_directories do
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
答案 0 :(得分:9)
您可能有权限问题:
在部署期间,capistrano尝试使用init脚本重新启动unicorn。 init脚本尝试将HUP信号发送给主设备,以通知它重新启动其工作程序。如果失败,则脚本假定unicorn未运行并尝试启动新实例。发生这种情况时,您会收到正在经历的错误(当新实例尝试启动时,它会注意到已经运行的实例并以您获得的错误终止)。
因此,在您的情况下,将HUP信号发送给独角兽大师似乎失败了。我怀疑这是由于许可问题。请检查unicorn是否由您在部署期间使用的同一用户启动(您的配置看起来正确,使用部署者到处都是,但是当前运行的独角兽进程可能是由root或其他人启动的? )。
对于疑难解答,我建议如下:
ps
或top
)APP_ROOT/tmp/pids/unicorn.pid
中且包含unicorn master的当前pid sudo su deployer
然后kill -HUP PID_OF_UNICORN_MASTER
)/etc/init.d/unicorn_xxx restart
作为部署者 我再次快速扫描您的deploy.rb,发现可能不正确的内容:请确保所有版本之间共享目录APP_ROOT/tmp/pids
。否则,unicorn会在一个版本中将其pidfile写入此子目录,当您部署新版本时,unicorn无法找到pidfile,因为它位于旧版本的子目录中...
如果您使用的是capistrano版本3,则可以通过包含类似
set :linked_dirs, [... 'tmp/pids' ...]
在deploy.rb中...
要在部署期间正确重新启动,unicorn需要在配置文件中指定的位置使用有效的pidfile。此外,运行独角兽的用户需要适当的权限才能写入此pid文件。
Rails应用已成为将pidfiles存储在APP_ROOT/tmp/pids
目录中的标准。
由于capistrano为每个部署创建了一个新的(app)目录,因此需要一些技巧来保持所有版本/部署都可以访问共享数据。它通过将几个目录(包括tmp/pids
)链接到共享位置中的目录来实现。
如果所有这一切都正确发生,那么重启unicorn将正常工作。
(我会在问题完全解决后立即更新此答案。请阅读此答案的评论中的讨论......)
答案 1 :(得分:1)
您是否在通过SSH连接到服务器时尝试运行sudo update-rc.d unicorn_myapp1 defaults
?这在过去已经解决了我的错误。
============更新============
我已经包含了一个我使用的deploy.rb文件,该文件从未给我任何问题。看看它是否适合你,显然做了一些编辑。我现在非常忙,所以我没有时间将它与你现有的deploy.rb文件进行比较,但也许这会提供一些指导 -
require "bundler/capistrano"
# YOU WILL NEED TO ADD THE LINE FOR RVM HERE
server "162.xxx.xxx.xxx", :web, :app, :db, primary: true
set :application, "APPLICATION-NAME"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git@github.com:GITHUB-USER-NAME/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
答案 2 :(得分:0)
基于您的重启命令
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD" # this is just running start
HUP信号正在退出并出现错误,如Cap错误中的“无法重新加载”消息所示,然后将运行正在运行的启动命令的下一行,因为您的错误日志告诉您。 (FWIW始终相信您的日志记录为起点)
您必须诊断HUP无法正常工作的原因。您可能必须切换到按此顺序发送信号USR2 - &gt;绞车 - &gt;退出并利用退出状态使其正常工作。 : - /
FWIW当我使用Unicorn时,这是我的control script ...虽然它在重启期间随机失败。但消息传递更冗长,应该有所帮助。
答案 3 :(得分:-1)
答案非常简单:您已经运行了服务器,其PID为20755。
您需要先使用sudo kill 20755
将其终止
然后尝试重新运行unicorn命令。