rake命令不是从cron作业运行,而是以其他方式运行

问题描述 投票:2回答:3

操作系统:安装在Docker上的Ubuntu和安装在我个人笔记本电脑上的普通ubuntu。

我正在运行这个cron工作:

*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"

我已经尝试了捆绑exec rake规范,没有/ bin / bash,每当gem,使用/ usr / local / bin / rake规范,但除了这些错误之外仍然没有发生任何事情。

运行cron作业时日志文件中的错误:

/bin/bash: bundle: command not found


/bin/bash: rake: command not found


rake aborted!
Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)


    bundler: failed to load command: rake (/usr/local/bin/rake)
        Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:95:in `block in materialize'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `map!'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `materialize'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:140:in `specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:185:in `specs_for'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:174:in `requested_specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/environment.rb:19:in `requested_specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:14:in `setup'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:95:in `setup'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/setup.rb:19:in `<top (required)>'
          /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
          /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

从命令行手动运行rake spec它工作正常,但从cron它给出了这个错误。有任何想法吗 ?

ruby-on-rails cron rake
3个回答
2
投票

我很确定这个问题是由于没有正确设置环境引起的。基本上cron在最小环境下运行,所以它不知道来自例如.bashrc文件的设置(你有rvmrbenv初始化和PATH设置)。

使用.来源环境文件,因此将cron条目重写为:

*/5 * * * * /bin/bash -l -c ". /etc/environment && cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"

请注意,/etc/environment只是一个例子。你可以找到像.bashrc这样的文件。


0
投票

使用whenever gem - 这是cron的适配器,支持rake任务和其他ruby / rails事物。


0
投票

我使用whenever gem来管理crontab。

添加

ENV.each { |k, v| env(k, v) }

config/schedule.rb文件为我解决了这个问题。

参考:https://github.com/javan/whenever/issues/656#issuecomment-239111064

© www.soinside.com 2019 - 2024. All rights reserved.