Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake command not running from cron job but works otherwise

Operating System: Ubuntu installed on a docker and a normal ubuntu installed on my personal laptop.

I am running this cron job:

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

I have tried it with bundle exec rake spec, without /bin/bash, with whenever gem, with /usr/local/bin/rake spec, but still nothing is happening except these errors.

Errors in the log file on running the cron job:

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

On running rake spec manually from the command line its working fine, but from cron it is giving this error. Any ideas ?

like image 331
user1735921 Avatar asked Sep 02 '25 06:09

user1735921


2 Answers

I am pretty sure that the issue is caused by not correctly set environment. Basically cron runs with the minimal environment so it is not aware about settings from for example .bashrc file (where you have rvm or rbenv initialization and PATH settings).

Source your environment file using . so rewrite the cron entry to something like:

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

Note that /etc/environment is just an example. You can source files like .bashrc instead.

like image 97
Maciej Małecki Avatar answered Sep 04 '25 21:09

Maciej Małecki


I use whenever gem to manage crontab.

Adding

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

to the config/schedule.rb file solved this issue for me.

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

like image 27
Vishal Avatar answered Sep 04 '25 20:09

Vishal