3

In crontab I have:

0,30 * * * * sh /path/to/my/script.sh

and in my script.sh I have:

#!/bin/bash
rvm use 1.9.2@r321
cd /path/to/my/proyect
rails runner rails_script.rb

But it doesn't load the rvm in 1.9.2, It stays on the system setting which is 1.8.7 in my case.

What should I do to make sure the runner is running under rvm in 1.9.2 with the gemset r321?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85

2 Answers2

6

there are at least 4 ways to make cron working with RVM: https://rvm.io/integration/cron/

personally I prefer the 2. Loading RVM environment files in shell scripts.

mpapis
  • 52,729
  • 14
  • 121
  • 158
  • Well when I run `$ rvm env --path -- 1.9.2@r321` it returns `/home/ubuntu/.rvm/environments/ruby-1.9.2-p290@r321` then I edit my script and add `source /home/ubuntu/.rvm/environments/ruby-1.9.2-p290@r321` but when I run the script it says: `source: not found`, any idea why? – Mr_Nizzle Feb 22 '12 at 17:51
  • Yes, the file `ruby-1.9.2-p290@r321` is located under `/home/ubuntu/.rvm/environments/` – Mr_Nizzle Feb 22 '12 at 19:38
  • is it the same machine ? copy-paste that line into your terminal and check if it changes `rvm current` – mpapis Feb 22 '12 at 21:07
  • Yes it is in the same machine, now if I try tu run the script with `sh /path/to/script.sh` gives me the error, but if i run this with `./path/to/script.sh` works perfectly. what's up with that? – Mr_Nizzle Feb 22 '12 at 21:43
  • sh is limited shell, run it with bash/zsh – mpapis Feb 22 '12 at 22:52
1

I think your problem is: RVM worked only correct in your user shell. Remember after installing rvm you must write somethink like.

'[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

in your .zshrc or .bashrc

Tell cron with wich user you use rvm because rvm must load as a function and with a user that not load rvm as a function it will not worked and the system ruby version is used automatically. Example:

0,30 * * * * USERNAME bash /path/to/my/script.sh

alternativly you can install rvm as multiuser show at: enter link description here

bulleric
  • 2,077
  • 6
  • 21
  • 36