crontab dont execute require - ruby

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.

Related

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

ruby command line LoadError

When I try to include a library on the command line, I receive LoadError messages
$ ruby -v
ruby 1.8.7 (2012-06-29 patchlevel 370)
$ gem list | grep coderay_bash
coderay_bash (1.0.2)
$ ruby -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
$ ruby -rubygems -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
It looks to work with ruby 1.9.2
$ ruby -v
ruby 1.9.2p290 (2011-07-09)
$ ruby -rcoderay_bash /bin/coderay -v
CodeRay 1.0.7
In Ruby 1.8, anything you want to require that was installed with RubyGems cannot be accessed until you require 'rubygems'. 1.9 removes this requirement.
You have several options for this:
Just put require 'rubygems' at the top of your file. This is harmless for 1.9 and is probably the easiest thing, because it's in the code and no one using your app has to remember anything
Change your shebang line to #!/usr/bin/env ruby -rubygems This tells the Ruby interpreter to require rubygems, but allows users to avoid this by sending your file to ruby directly, if they are offended by RubyGems for some reason
Always run with ruby and use -rubygems, e.g. ruby -rubygems my_app.rb This has no dependencies on RubyGems in your code, and will work, but you have to remember to do it everytime, which is somewhat of a pain.

Install non-gem library in Ruby

How do you install a library that is not a gem in Ruby?
I'm trying to use graphy.
In the example usage, it says to require 'graphy', but even when my ruby file is in the same directory as graphy.rb, I get the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- graphy.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from foo.rb:1:in `<main>'
Tell ruby to look in the current dir using the -I flag:
ruby -I. my_script.rb
To see the ruby load path, add puts $: at the top of your script.
Everything works fine. I have pasted my IRB try below.
Yasky$ cd Projects/ruby/bruce-graphy-70f213b/lib/
lib Yasky$ irb
ruby-1.8.7-p352 :001 > require 'graphy'
=> true
ruby-1.8.7-p352 :002 > dg = Graphy::Digraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6]
=> Graphy::DirectedGraph[Graphy::Arc[2,3,nil], Graphy::Arc[1,6,nil], Graphy::Arc[2,4,nil], Graphy::Arc[4,5,nil], Graphy::Arc[1,2,nil], Graphy::Arc[6,4,nil]]
ruby-1.8.7-p352 :003 > exit
lib Yasky$
Explicitly specifying your current directory in your load path may do the trick.
EDIT: Oops, I was too late (:
In this particular case, the author is using Jeweler to manage his gemspec. I'm not that familiar with Jeweler, but AFAIR, you generate and install a Gem with rake install. You may need to generate a version number first with rake version:write MAJOR=0 MINOR=0 PATCH=1.

Ruby cannot find required libraries even though gem is installed

I have spent literally days trying to install ruby 1.9.2 and get it working with gems :-/ I eventually gave up on my Mac OSX 10.6 machine and below is the current state on my Ubuntu machine. Any advice would be greatly appreciated!
# ruby test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mongo (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:1:in `<main>'
# cat test.rb
require 'mongo'
db = Mongo::Connection.new.db("mydb")
# gem which mongo
/usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongo-1.1.2/lib/mongo.rb
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
According to this page: http://docs.rubygems.org/read/chapter/19
I symlinked which ruby I was using to match that which gem is using:
# which ruby
/usr/local/rvm/bin/ruby
# ls -l `which ruby`
lrwxrwxrwx 1 root root 44 2010-11-17 13:25 /usr/local/rvm/bin/ruby -> /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# which gem
/usr/local/rvm/bin/gem
# gem -v
1.3.7
# ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
Try putting the following line at the beginning
require "rubygems"
Why is "rvm" displaying in your /usr/local/rvm/ path? Did you do a system-wide install, as a system administrator using administering Ruby system wide for multiple users?
Did you add [[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm' to your ~/.bashrc, ~/.bash_profile or ~/.profile (whichever you have configured)?
For normal, every day use, I recommend RVM's default setup:
RVM installation, RVM gems management.
Note to self: Buy stock in RVM. It's too cool.
Does it work under Ruby 1.8.7, which is pre-installed by default on OS X?
If so, one difference between 1.9.1 and 1.9.2 is that "." isn't part of $:'s path any more.
I recommend that you do rvm implode and delete the current setup. Then use the railsready script to setup RVM and Ruby properly for you on Ubuntu. It's important to understand that until you know what you are doing you should run the script as a user. Hope that helps.
On linux and OS X, I have always had to put require "rubygems" in the beginning. However it has always worked fine without this line on windows.

I see gem in "gem list" but have "no such file to load"

I am on Ubuntu10
sudo apt-get install ruby1.9.1-full
then download sources of rubygem 1.3.7 and install it
sudo ruby setup.rb
then, for example, install sinatra
sudo gem install sinatra
Finally open irb and type
require "rubygems"
require "sinatra"
and get error
LoadError: no such file to load -- sinatra
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I had exactly this problem. The problem is that gem and ruby disagree about where the gems live. Compare these:
ruby -e "puts Gem.path"
gem env
gem which sinatra
If you're like my setup, you'll notice that there's an entry in gem env's paths that isn't in Gem.path, and that's exactly where sinatra will claim to be. In my case, I had to add
export GEM_HOME=/usr/lib/ruby/gems/1.9.1
to my .profile. Then everyone was happy.
Execute
sudo gem install sinatra --verbose
and note the path where the gem is getting installed.
Then try this in irb
puts $LOAD_PATH
and make sure that gem is installed in one of the directories in $LOAD_PATH
And ideally just start using http://rvm.beginrescueend.com/
I usually hit this error when I forget:
require 'rubygems'
It'd be helpful if you provided the actual code sample, though, what gem you want to require, and what Ruby version you're using if this doesn't solve the problem.
This was before here on SO quite a few times. Problem is that you probably have two versions of ruby. The one is installing the gem and the other one is trying to use it. Do this in terminal:
$ which -a ruby
Or this:
$ which -a gem
to see if you have more than one version of ruby/gem installed. If so - remove one version (via $ rm or package manager of your system).
I use ruby gems 1.8.7 for a project. I was getting the same error. Use the line require 'rubygems'. It must always be the first require statement, otherwise you can get an error. In my code, I had
require 'watir'
require 'rubygems'
# more code
I got the error - in `require': no such file to load -- watir (LoadError).
When I put rubygems first, the error went away and everything worked. I don't know
why this happens.
Btw, I tried user24359 answer and it did not help me.
C:\code>ruby -e "puts Gem.path"
-e:1: uninitialized constant Gem (NameError)

Resources