rake 命令不是来自 cron 作业的 运行,但可以正常工作
rake command not running from cron job but works otherwise
操作系统:Ubuntu 安装在 docker 上,正常 ubuntu 安装在我的个人笔记本电脑上。
我是 运行 这个 cron 作业:
*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
我已经尝试使用 bundle exec rake spec,没有 /bin/bash,whenever gem,/usr/local/bin/rake spec,但除了这些错误之外仍然没有任何反应。
运行 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 上它给出了这个错误。
有什么想法吗?
使用 whenever gem - 这是 cron 的适配器,支持 rake 任务和其他 ruby/rails 东西。
我很确定问题是由未正确设置的环境引起的。基本上 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
之类的源文件。
我用whenever
gem来管理crontab
添加
ENV.each { |k, v| env(k, v) }
config/schedule.rb
文件为我解决了这个问题。
参考: https://github.com/javan/whenever/issues/656#issuecomment-239111064
操作系统:Ubuntu 安装在 docker 上,正常 ubuntu 安装在我的个人笔记本电脑上。
我是 运行 这个 cron 作业:
*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
我已经尝试使用 bundle exec rake spec,没有 /bin/bash,whenever gem,/usr/local/bin/rake spec,但除了这些错误之外仍然没有任何反应。
运行 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 上它给出了这个错误。
有什么想法吗?
使用 whenever gem - 这是 cron 的适配器,支持 rake 任务和其他 ruby/rails 东西。
我很确定问题是由未正确设置的环境引起的。基本上 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
之类的源文件。
我用whenever
gem来管理crontab
添加
ENV.each { |k, v| env(k, v) }
config/schedule.rb
文件为我解决了这个问题。
参考: https://github.com/javan/whenever/issues/656#issuecomment-239111064