Diogo Amaral Diogo Amaral - 1 year ago 111
Ruby Question

crontab dont execute require

I'm using a osx and I created a ruby script in the path:

with name

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
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download