繁体   English   中英

在gem错误时使用rails脚本:__rvm_add_to_path:找不到命令

[英]rails script using whenever gem error: __rvm_add_to_path: command not found

我正在使用capistrano和every gem。

Capistrano使用schedule.rb文件中详细介绍的cronjob在我们的ubuntu服务器上部署和更新crontab。

但是cronjob不会发生。 它失败并每5分钟通过电子邮件向我发送此错误:

/etc/profile.d/rvm.sh: line 67: __rvm_add_to_path: command not found
/home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
    from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /[REMOVED]/releases/20140204194143/config/boot.rb:6:in `<top (required)>'
    from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/pkatepalli/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from script/rails:4:in `<main>'

奇怪的是,我们使用与pkatepalli不同的用户名进行部署。 pkatepalli是我在服务器上的用户名。 这就解释了为什么它会通过电子邮件向我发送错误-我认为。

另外,我们使用的是ruby 1.9.3,而不是1.9.1。 我不确定为什么没有使用正确的红宝石版本。

在服务器上:

Bundler version 1.3.5

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

$ rbenv version

1.9.3-p448 (set by /[REMOVED]/current/.ruby-version)

但有趣的是,在当前目录中有ruby 1.9.1,我对此感到困惑:

/current/vendor/bundle/ruby/1.9.1 

的Gemfile:

gem 'whenever', require: false

schedule.rb: 我尝试取消注释:env'PATH',ENV ['PATH'],但是放在schedule.rb文件中的PATH没有帮助。 在本地,我们使用RVM代替rbenv(服务器上正在使用rbenv)。 我不确定是否设置了该路径:env'PATH',ENV ['PATH']

#env 'PATH', ENV['PATH']

#set :output, "/log/cron.log"
#set :stage, :environment_variable

every 5.minutes, :roles => [:app] do
  runner "Model.method"
end

deploy.rb:

require 'capistrano/log_with_awesome'
require "bundler/capistrano"

set :application, "[REMOVED]"

set :scm, :git 

set :repository,  "[REMOVED]"

set :branch, "master"
set :deploy_via, :remote_cache 

set :user, "[REMOVED]"
set :password, "[REMOVED]"
set :deploy_to, "[REMOVED]"

set :keep_releases, 5

task :qa do

    set :domain, "[REMOVED]"
    role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
    role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
    set :env, "test"
end


task :production do

    set :domain, "[REMOVED]"
    role :web, "[REMOVED]", {:port => [REMOVED]} # Your HTTP server, Nginx
    role :app, "[REMOVED]", {:port => [REMOVED]} # This may be the same as your `Web` server
    set :env, "production"
end


set :use_sudo, false
default_run_options[:pty] = true


role :db, "[REMOVED]", {:port => [REMOVED], primary: true, :no_release => true}

after "deploy:setup", "deploy:chown"

namespace :bundle do 

    task :install, {:roles => :app} do
        run "cd #{release_path} && bundle install --deployment --without development test"
    end

end

before "deploy:assets:precompile" do

    transfer :up, "config/application.yml", "#{shared_path}/application.yml", :via => :scp

    run "ln -nfs #{shared_path}/application.yml #{release_path}/config/application.yml"

end

namespace :whenever do
  task :start, :roles => :app do
    run "cd #{release_path} && bundle exec whenever --update-crontab"
  end
end

namespace :deploy do 

    task :execute_migrations, :roles => :app do
        puts "RUNNING DB MIGRATIONS"
        run "cd #{current_path}; bundle exec rake RAILS_ENV=#{env} db:migrate"
    end

    task :chown do
        run "#{try_sudo} chown -R #{user} #{deploy_to}"
    end

    task :restart_nginx, {:roles => :web} do
        run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
    end

    before "deploy:restart_nginx", "deploy:execute_migrations"

    after :deploy, "deploy:restart_nginx"

    after "deploy:restart_nginx", "deploy:cleanup"

    after "deploy:update", "whenever:start"


end

当我SSH进入服务器并在应用程序的当前目录中运行crontab -l时:

# Begin Whenever generated tasks for: /[REMOVED]/releases/20140204194143/config/schedule.rb
#0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /[REMOVED]/releases/20140204194143 && script/rails runner -e production '\''Model.method'\'''

# End Whenever generated tasks for: [REMOVED]/releases/20140204194143/config/schedule.rb

当我像这样在服务器上运行cron作业时,我得到以下结果:

:/[REMOVED]/current$ rails runner -e production '\''Model.method'\'''
Rails Error: Unable to access log file. Please ensure that /[REMOVED]/releases/20140204194143/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': (eval):1: syntax error, unexpected $undefined (SyntaxError)
\Model.method'
 ^
(eval):1: unterminated string meets end of file
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:5:in `require'
    from script/rails:5:in `<main>'

当我这样做的时候。 我得到这个:

[REMOVED]/current$ rails runner Model.method
Rails Error: Unable to access log file. Please ensure that [REMOVED]/releases/20140204194143/log/development.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
[REMOVED]/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
    Is the server running on host "localhost" ([REMOVED]) and accepting
    TCP/IP connections on port 5432?
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
    from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
    from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/model_schema.rb:308:in `clear_cache!'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/railtie.rb:97:in `block (2 levels) in <class:Railtie>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:418:in `_run__249672195884464632__prepare__2220886522034318467__callbacks'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /[REMOVED]/releases/20140204194143/config/environment.rb:5:in `<top (required)>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:103:in `require_environment!'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:44:in `<top (required)>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:5:in `require'
    from script/rails:5:in `<main>'

回应列昂尼德·舍甫佐夫的回答:

我跑了:

rails runner -e production 'Model.method'

我得到了这个:

[REMOVED]/current$ rails runner -e production 'Model.methodName'
/[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval': undefined method `methodName' for #<Class:0x0000000570d668> (NoMethodError)
    from (eval):1:in `<top (required)>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `eval'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/runner.rb:53:in `<top (required)>'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `require'
    from /[REMOVED]/releases/20140204194143/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:5:in `require'
    from script/rails:5:in `<main>'

当我运行crontab -l时,我得到了每当生成的克朗

a)1.9.1是Ruby 1.9.3的“基础”版本(1.9.3被认为是Ruby 1.9.1的版本); 在路径中看到它是正常的。

b)cron的命令行中转义了引号; 手动检查它的正确方法是rails runner -e production 'Model.method'

c)您确定错误不是来自本地安装的cron作业吗? (使用crontab -l检查)

好的,这有点复杂,并且缺少一些调试信息,所以我将尝试猜测。

  • 首先,看来您的系统上同时安装了rvmrbenv ,这不是一个好主意 尝试卸载二者之一,或者最好同时卸载两者,然后重新安装您喜欢的一种。

  • 在您的部署脚本中,似乎每次capistrano任务时您都已滚动。 这还不错,但是whenever已经提供了更好的支持(例如回滚)就可以了。 您还可以获得关于您将来问题的更好的反馈。

这些是潜在的陷阱,但它们可能不是造成问题的原因。 您似乎对linux用户及其权限感到困惑。 据我了解,您的生产系统上有两个用户: pkatepalli和另一个您注释掉的用户,我们称他为deployer

从我的角度来看,您正在尝试从deployer的角度来设置所有功能,这是一个好习惯。

  • 我假设capistrano user变量设置为deployer 如果不是这样,这就是问题的根源。 Capistrano使用该用户登录并发出命令。
  • 同样,当您运行crontab -l时,您似乎可以正确看到cron作业, 但是如果您在运行命令时使用pkatepalli登录,那么这就是您的问题。 您已将任务写入错误用户的crontab。 如果发生这种情况,则很可能意味着您在cap部署期间从错误的用户运行了whenever命令(在这种情况下,您需要检查在cap期间是哪个登录用户),或者实际上已经从两个用户运行了whenever命令而且您只会得到pkatepalli错误(请在再次尝试之前清除crontab)。 如果它仍然看起来这是确定,那么你可以尝试传递-u username选项, whenever命令(您可能需要sudo了这一点)。
  • 要正确设置deployer用户,您可以按照capistrano的说明进行操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM