2

I set up a cron job in my schedule.rb which just executes a custom rake file. Here is my schedule.rb:

set :environment , :development
    every 2.minutes do
     rake "runpy" , :output => {:error => "/home/aditya9509/Desktop/rubystack-2.0.0-33/projects/myApp/log/error.log" , :standard => "/home/aditya9509/Desktop/rubystack-2.0.0-33/projects/myApp/log/cron.log"}
    end 

Note that runpy is a custom rake file and it works fine when ran from the terminal using the command:

rake runpy

I have set standard output to cron.log and error output to error.log When running the cron job, there is no error printed in error.log but the following in cron .log:

In Gemfile:
rails (= 4.2.4) depends on
  bundler (< 2.0, >= 1.3.0)

  Current Bundler version:
    bundler (1.0.15)

So i checked the current bundler version using the command

bundler --version 

which outputs

Bundler version 1.11.2

Further, I thought I would check for the list of available bundlers so that I can uninstall the 1.0.15 version used by the cron so that it automatically uses the 1.11.2 version. I ran the following command:

gem list | grep "bundler" 

To my surprise, the 1.0.15 version being used by cron is not installed on my system. Here was the output of the above command:

bundler (1.11.2, 1.3.6)
hoe-bundler (1.2.0)

Do you know what is going on? I am not sure as to what information would be relevant so let me know, I shall update my question accordingly. I am using the whenever gem to set up a cron job.

Aditya Naidu
  • 697
  • 2
  • 7
  • 18

1 Answers1

0

Cron doesn't run in an interactive shell and therefoe doesn't load .bashrc. If you're using RVM or rbenv or similar, it's likely that cron is using system Ruby, instead of the latest Ruby you've installed yourself.

To test this, try running which ruby or ruby -v from cron. Is it as you expect?

If not, try using a shebang at the top of your bin/rails file to coerce Rails into using the proper ruby version. For instance if you're using ruby 2.2.4:

At the top of bin/rails

#!/Users/<username>/.rvm/rubies/ruby-2.2.4/bin/ruby

Obviously, substitute whatever path you're using for Ruby.

Anthony E
  • 11,072
  • 2
  • 24
  • 44