6

I'm trying to run a simple ruby script on my old PPC machine running 10.5 in an RVM environment.

Searching on SO, I've followed the chosen answer from this post.

This is the line in cron as a result:

SHELL=/bin/bash
00 * * * * BASH_ENV=~/.bash_profile && /bin/bash -c '~/deggy/onlineGW.rb'

This command runs fine in Bash at the root of the user sam.

Here's the salient part of my script:

 #!/usr/bin/env ruby
 require 'open-uri'
 require 'nokogiri'
 ...

Here's the output of the error from cron:

 X-Cron-Env: <SHELL=/bin/bash>
 X-Cron-Env: <PATH=/usr/bin:/bin>
 X-Cron-Env: <LOGNAME=sam>
 X-Cron-Env: <USER=sam>
 X-Cron-Env: <HOME=/Users/sam>
 Date: Mon,  6 Jan 2014 03:15:00 -0600 (CST)
 /Users/sam/deggy/onlineGW.rb:3:in `require': no such file to load -- nokogiri (LoadError)

OK, since I'm running RVM I have set my default ruby to 1.9.3 and as I mentioned above, the command executes in Terminal but not in cron. Is there another environment in play?

So clearly, there's something I'm overlooking. Help me to see it, sam

Community
  • 1
  • 1
sam452
  • 1,281
  • 3
  • 20
  • 33

3 Answers3

31

Or you can simply try

rvm cron setup # let RVM do your cron settings

which worked for me. via https://coderwall.com/p/vhv8aw

hagope
  • 5,523
  • 7
  • 38
  • 52
Michael
  • 1,922
  • 1
  • 19
  • 17
7

I configured several different operating systems to work with a couple of CRON flavors and RVM.

I first tried RVM's official solution to the problem but didn't work under FreeBSD and Gentoo. I had to manually add all relevant paths as showed bellow but first type crontab -e in order to launch the crontab editor[1]:

# atmat's crontab configuration
SHELL=/bin/bash
PATH=/home/atma/.rvm/gems/ruby-1.9.3-p0/bin:/home/atma/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/atma/.rvm/rubies/ruby-1.9.3-p0/bin:/home/atma/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i486-pc-linux-gnu/gcc-bin/4.5.3
RUBYLIB=/home/atma/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1
GEM_HOME='/home/atma/.rvm/gems/ruby-1.9.3-p0'
GEM_PATH='/home/atma/.rvm/gems/ruby-1.9.3-p0:/home/atma/.rvm/gems/ruby-1.9.3-p0@global'
RUBYOPT=rubygems

%nightly,mail(no) * 8-9 /home/atma/.rvm/rubies/ruby-1.9.3-p0/bin/ruby  /usr/local/bin/morula -s username update

The above example is working under Gentoo GNU/Linux using fcron a more flexible, beautiful and powerful solution to standard cron, but will work with any cron.

[1] This command will open crontab with your default system editor.

patm
  • 1,420
  • 14
  • 19
  • One of the things that I've not read is WHERE the config for crontab is. Where would I update my configuration? – sam452 Jan 06 '14 at 17:00
  • I believe the equivalent of your config lines are pasted at the top of the crontab. I get a little closer as my error returns `read_nonblock': Connection reset by peer. Of course, in terminal using BASH_ENV=~/.bash_profile && /bin/bash -c '~/.rvm/rubies/ruby-1.9.3-p0/bin/ruby ~/deggy/onlineGW.rb' works so it's not likely the remote server blocking it. – sam452 Jan 06 '14 at 23:39
  • I added the requested info. – patm Jan 07 '14 at 18:53
  • I was having a similar issue where a shell script (run from cron) was using ruby. I had to do `export ` for it to work, just in case others encounter the same. – Wayne Weibel Jan 28 '14 at 21:22
  • @WayneWeibel What OS and Shell are you using? – patm Jan 30 '14 at 12:23
  • @atomsx that was part of the problem actually, the shell for cron was not the same as the user (we think) and I tested on a Mac, but our servers are CentOS and Ubuntu with different ruby installs on top of the rvm shenanigans ... as you can tell it is a bit of a cluster-(you know) – Wayne Weibel Jan 30 '14 at 14:54
0

To load default RVM ruby environment for cron jobs, here is what I setup for user-mode RVM, assume the user is ohho, home folder is /home/ohho. To edit, enter crontab -e in command line:

MAILTO=""
SHELL=/bin/bash
BASH_ENV=/home/ohho/.bash_profile
HOME=/home/ohho

* * * * * rails -v > /home/ohho/env.txt

The last line is for testing rails (if installed) can be invoked properly. You should also check whether ~/.bash_profile loads the RVM environment (which is default for bash).

A detail explanation of SHELL and BASH_ENV can be found in Daniel's answer.

Community
  • 1
  • 1
ohho
  • 50,879
  • 75
  • 256
  • 383