当 Cron 运行 时 Rake 任务失败

Rake task fails when ran by Cron

我有一个 rake 任务,当我从应用程序目录中的命令行 运行 它时,它工作正常,但是当 cron 运行s 它输出以下错误时:

/var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find spring-2.1.0 in any of the sources (Bundler::GemNotFound)
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `map!'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `materialize'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:170:in `specs'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:237:in `specs_for'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:226:in `requested_specs'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:108:in `block in definition_method'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:20:in `setup'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

这是 cronjob:

*/15 * * * * cd /var/www/myapp && /usr/local/bin/bundle exec /usr/local/bin/rake myapp:heartbeat RAILS_ENV=production > /var/www/myapp/log/heartbeat.log 2>&1

cronjob 过去一直在工作,但我最近将 ruby 升级到 2.6,我想这个过程中出现了一些问题。

如果我 运行 从应用程序文件夹外得到以下内容:

$ bundle show spring
Could not locate Gemfile or .bundle/ directory

但从应用程序文件夹中我得到:

$ bundle show spring
/var/www/myapp/vendor/cache/ruby/2.6.0/gems/spring-2.1.0 

但考虑到 cronjob 首先 cd 进入应用程序文件夹,我想这应该不是问题。知道发生了什么以及如何修复它吗?

更多详情:

$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux-gnu]

$ bundle -v
Bundler version 1.17.3

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.0.3
  - RUBY VERSION: 2.6.5 (2019-10-01 patchlevel 114) [x86_64-linux-gnu]
  - INSTALLATION DIRECTORY: /var/lib/gems/2.6.0
  - USER INSTALLATION DIRECTORY: /home/julien/.gem/ruby/2.6.0
  - RUBY EXECUTABLE: /usr/bin/ruby2.6
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - SPEC CACHE DIRECTORY: /home/julien/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /var/lib/gems/2.6.0
     - /home/julien/.gem/ruby/2.6.0
     - /usr/lib/x86_64-linux-gnu/rubygems-integration/2.6.0
     - /usr/share/rubygems-integration/2.6.0
     - /usr/share/rubygems-integration/all
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/julien/bin
     - /home/julien/.local/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial

所以终于成功了,我在 crontab 中设置了 env 变量并从作业命令中删除了完整路径,它终于成功了,这是工作版本的工作方式:

/etc/crontab

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root

*/15 * * * * root cd /var/www/myapp/ && bundle exec rake myapp:heartbeat RAILS_ENV=production > /var/www/myapp/log/heartbeat.log 2>&1