Search code examples
ubunturuby-on-rails-4cronrakebundler

Rake task fails when ran by Cron


I have a rake task that works fine when i run it from the command line from within the app's directory, but when cron runs it it outputs the following error:

/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'

this is the 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

the cronjob was working in the past but i recently upgraded ruby to 2.6 and i guess something got messed up in the process.

if i run the following from outside of the app folder i get:

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

but from inside the app folder i get:

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

but considering the cronjob first cds into the app folder this shouldn't be a problem I guess.. Any idea what's going on and how to fix it?

More details:

$ 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

Solution

  • So finally managed to make it work, i set the env variables in the crontab and removed the full paths from the job's command and it finally worked, here's how the working version worked:

    /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