Ruby - No gem to load in cronjob - ruby

I use rvm. and ruby 1.9.2 is installed in rvm.
I have a script and it runs fine in terminal.
I have wrote a cron job for the same script:
27 * * * * su - sayuj -c 'cd /path/to/dir; /home/sayuj/.rvm/rubies/ruby-1.9.2-p136/bin/ruby script.rb >> /var/log/script.log 2>&1'
Then it shows no gem is to load error. I think the cron is not referring to the rvm ruby 1.9.2.
How can I fix this issue?

You need to change cd /path/to/dir; to the real directory.
Then, create a file named .rvmrc with this content inside:
rvm use 1.9.2
It'll execute every time you enter the directory, setting to the correct version of ruby.

Related

turn off ruby version warning

Ubuntu 18.04, ruby 2.6.6
Cron jobs submitted for redmine generate warnings that are then mailed to admin, creating a steady stream of emails every 15 minutes:
Subject: Cron <root#mysys> su -l rubyuser -c " cd /opt/redmine/redmine ; bundle exec rake --trace redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com port=993 ssl=1 username=issueadmin#someplace.com password=pw folder=Inbox move_on_success=Processed project=Issues status=open tracker=Bug priority=Normal >> /opt/redmine/redmine/log/issues.log 2>&1"
The mail contains the usual ruby warning message:
Unknown ruby interpreter version (do not know how to handle): >=2.3.0,<2.7.0ifBundler::VERSION>=1.12.0.
I have tried turning off the warning using
rvm rvmrc warning ignore allGemfiles
su -l rubyuser -c " cd /opt/redmine/redmine ; rvm rvmrc warning ignore allGemfiles"
but it has no effect.
Can someone tell me how to fix this / turn this off?
This is a system-wide ruby, installed at /usr/local/rvm. It appears to be caused by the Gemfile installed at /opt/redmine/redmine, which contains the line
ruby '>= 2.3.0', '< 2.7.0' if Bundler::VERSION >= '1.12.0'
I've tried tweaking that line a bit but I still get the error.
Matthew Boeh gave me the following answer which solves the immediate issue:
It looks like this is an issue with RVM, which tries to read the
Gemfile to determine which version of Ruby to switch to. It's limited
in what it can parse. I believe if you put a .ruby-version or .rvmrc
in that directory it will ignore the Gemfile.
I added "2.6.6" to a .ruby-version for the project.
The reason may be slightly more nuanced, as rvm doesn't consider 2.6.6 an "official" release for some reason, which may be the reason it complains. It does consider 2.7.0 one. At least that's my recollection; don't have access to the machine right now and I would need to install a 2.7 to find out.

Missing gems for cronjob which are installed

On my server I have one cronjob for running ruby script through rake, but the script is not executed. I'm running rvm with 2.1.2 ruby and here is my crontab:
SHELL=/bin/bash
PATH=/home/deployer/.rvm/gems/ruby-2.1.2/bin:/home/deployer/.rvm/gems/ruby- 2.1.2#global/bin:/home/deployer/.rvm/rubies/ruby-2.1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/deployer/.rvm/bin:/home/deployer/.rvm/bin
*/1 * * * * cd /var/www/cars_crawler && /home/deployer/.rvm/gems/ruby-2.1.2/bin/rake >> /var/www/cars_crawler/cron.txt 2>&1
The error from cronjob is:
/home/deployer/.rvm/rubies/ruby-2.1.2/bin/ruby get_cars.rb
/home/deployer/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- nokogiri (LoadError)
When I run manually the rake command, not through crontab everything is ok, but from cronjob the problem is present. What I noticed is this line: /home/deployer/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/ I don't have ruby 2.1.0 installed. What can cause this problem, I'm out of ideas

Using whenever gem with sinatra

#config/scheduler.rb
set :output, 'log/cron.log'
every 1.minutes do
rake 'reports:generate'
end
When I run crontab -l I get the following output:
* * * * * /bin/bash -l -c 'cd <path-to-app> bundle exec rake reports:generate --silent >> log/cron.log 2>&1'
First off, it seems to use my system ruby which is 1.8.7, is there a way to change this?
Secondly, the task is not run, but the only thing that shows up in my cron.log is the following:
Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0
I've tried puts to the log with no success, so I have basically no idea what's going on. It seems the task is being initiated, but then fails without anything being written to the log. I have no idea how to go about debugging this, so please help.
I'm on OS X, and my application is running on Sinatra.
edit: I'm using rbenv to manage my ruby versions, so I guess I have to tell the cron job to load rbenv somehow? When I cd into the app's folder and run ruby -v I get
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
It seems the problem was with rbenv, and not necessarily sinatra or whenever. This solved my problem:
#schedlue.rb
command "cd #{Dir.pwd} && RACK_ENV=#{environment} rbenv exec bundle exec rake reports:generate"
By adding rbenv exec we successfully loaded the correct ruby version before running the rake task.

how to add rvm to super user

hi i need to execute a ruby program from a init script i need to run the script as
sudo /etc/bin/differ.sh start
but the problem is rvm is not installed on root(super user) . so code in the differ script ruby "filename" wont execute because the gems and rvm is not installed in root.
i need solve how to add rvm and its gems to root (i have not installed by system wide installation).i have even tried the rvmsudo
rvmsudo ruby "filename"
but it's not working in script works good in terminal.
please free to ask any files or outputs i will post along with this question.
This page on integrating RVM with init.d may help: https://rvm.io/integration/init-d

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

Resources