Search code examples
ruby-on-railsrubymacosrbenvwhenever

Rails Cron Job Not running for configured global ruby version MacOS


I am using rbenv to manage multiple ruby versions. I have the global and shell ruby version set as 2.7.6. I am using rails 7.0.4. My macos version is Monterey 12.5.1. rbenv global also outputs 2.7.6.

I am using the whenever gem to create a simple cron job. My schedule.rb looks like below

set :output, "./log/cron_log.log"

every 1.minute do
  runner "puts 'Hello, world'"
end

but in my crontab log file I am getting this

/Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:54:in `pwd': Operation not permitted - getcwd (Errno::EPERM)
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:54:in `lockfile_contents'
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:41:in `lockfile_version'
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:10:in `bundler_version'
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:17:in `block in prioritize!'
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:17:in `find_index'
    from /Library/Ruby/Site/2.6.0/rubygems/bundler_version_finder.rb:17:in `prioritize!'
    from /Library/Ruby/Site/2.6.0/rubygems/dependency.rb:282:in `matching_specs'
    from /Library/Ruby/Site/2.6.0/rubygems.rb:257:in `find_spec_for_exe'
    from /Library/Ruby/Site/2.6.0/rubygems.rb:284:in `activate_bin_path'
    from /usr/bin/bundle:23:in `<main>' 

The default shell for my macos version is zsh. But apparently the crons are run using bash -l -c. So I followed the instructions in rbenv's github page and setup .bash_profile like below

#.bash_profile
eval "$(rbenv init - bash)"

#.bashrc
source .bash_profile

But I am still getting the same output.Could anybody please help me with this?


Solution

  • For everyone facing similar problem on macos, give a try to github.com/rvm/rvm rvm. Switching the ruby version manager to rvm fixed this problem for me.