Missing gems for cronjob which are installed - ruby

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

Related

Running bundle exec rake inside cron cannot find specific version of rake

I am setting up cron to run some rake tasks for my ruby on rails server. They are generated by whenever and look like this:
* * * * * /bin/bash -l -c 'cd /var/www && RAILS_ENV=production /usr/local/bin/bundle exec /usr/local/bin/rake my:task --silent >> /var/www/log/cron.log 2>&1'
bundle and rake executables seem to be found, but inside cron.log I find this error:
bundler: failed to load command: /usr/local/bin/rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
/usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/spec_set.rb:91:in `block in materialize'
...
...
As I can see it tried to load rake-12.3.2, which is a correct version, from /usr/local/lib/ruby/gems/2.5.0/gems, which is not a correct path, because there is no rake-12.3.2. Correct version of rake is under another path /usr/local/bundle/gems, but I have no luck setting this path for cron to use.
I have tried adding GEM_PATH="/usr/local/bundle/gems" to cron file, but it doesn't work.
I am not using rvm or rbenv, ruby is installed directly.

create Scheduled Task from working Ruby script on Ubuntu inside Windows10

I have a Windows10 Ubuntu bash environment set up. It has a few rubygems installed (without docker, bundler, without rvm, and also this is not rails). The script is working fine when I run it manually from inside the Ubuntu terminal, in the user home folder ~/
Now my goal is create a Windows Scheduled Task using the Task Scheduler application, to run the script daily, like I would with a crontab in Unix.
The action in this Scheduled Task is the challenge. In testing manually from the Windows CMD prompt, I got as far as:
C:\>C:\Windows\System32\bash.exe -c "ruby ~/myscript.rb"
That fails :
Traceback (most recent call last):
2: from /home/lam/ruby/remind_prepare_dad_before_iterative_ends.rb:15:in `<main>'
1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- tiny_tds (LoadError)
Above error tells me it cannot find my gem, TinyTDS. What do I add to the command line to make it find my gems?
I found a crude workaround for my own problem: just install the gem from the same command line:
C:\>C:\Windows\System32\bash.exe -c "sudo gem install tiny_tds"
I had to re-install a few more gems I use in the same way.
After this, I put in a -C argument to start ruby in the right directory, like so:
C:\>C:\Windows\System32\bash.exe -c "ruby -C ~/ruby myscript.rb"
works like a charm.
I suppose now I have duplicated gems floating in spaces on the machine, but we will address that issue if it ever becomes a problem. :-)
Try bundle exec ruby ~/myscript.rb

crontab dont execute require

I'm using a osx and I created a ruby script in the path: /Users/diogo/workspace/outros/crawler_trf with name get_news.rb
So I tried to execute it via crontab with the following line: */1 * * * * 'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out and I've got the error: /bin/sh: ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb: No such file or directory
After some searches and a question here I solved this problem removing the quotes and now my crontab looks like that: */1 * * * * cd /Users/diogo/workspace/outros/crawler_trf/ && ruby get_news.rb > /tmp/crawler_trf.out
But now I'm having a new error that is:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mail (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from get_news.rb:3:in `<main>'
The beginning of my file is:
require 'nokogiri'
require 'open-uri'
require 'mail'
My ruby version is 2.3.0 and I have it specified in .ruby-version and using gemset specified in `.ruby-gemset
I installed the gems via bundle
I really had searching for my answers for long but I didn't found nothing.
You should always fully specify the paths of all files when executing them out of cron, since the environment provided by cron will be different from your login environment. That might mean replacing the invocation of ruby with the full path to your ruby binary (maybe /usr/local/bin/ruby).
Try running ruby -v out of cron to see what version you are picking up. On my Mac OS system I have two:
Edwards-MacBook-Air:~ emv$ /usr/bin/ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
Edwards-MacBook-Air:~ emv$ /usr/local/bin/ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
one from the operating system, and one from Homebrew.

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.

Ruby - No gem to load in cronjob

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.

Resources