操作系统:安装在Docker上的Ubuntu和安装在我个人笔记本电脑上的普通ubuntu。
我正在运行这个cron工作:
*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
我已经尝试使用bundle 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发出此错误。
有什么想法吗?
答案 0 :(得分:2)
我很确定这个问题是由于没有正确设置环境引起的。基本上cron在最小环境下运行,因此它不知道例如.bashrc
文件的设置(您有rvm
或rbenv
初始化和PATH
设置。
使用.
来源环境文件,因此将cron
条目重写为:
*/5 * * * * /bin/bash -l -c ". /etc/environment && cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
请注意/etc/environment
只是一个例子。您可以改为使用.bashrc
等文件。
答案 1 :(得分:0)
使用whenever gem - 这是cron的适配器,支持rake任务和其他ruby / rails事物。
答案 2 :(得分:0)
我使用whenever
gem管理crontab。
添加
ENV.each { |k, v| env(k, v) }
转到config/schedule.rb
文件为我解决了此问题。
参考: https://github.com/javan/whenever/issues/656#issuecomment-239111064