RVM on Ruby Scripts - ruby

I need for a Ruby script to be run using an rvm-selected version. I cannot change how the script is invoked, but I can modify the script. The script starts with:
#!/usr/bin/env ruby
Now, based on some information I found (in this question, for example), I tried this:
#!/usr/bin/env rvm-shell ree-1.8.7-2012.02#gitorious
But this only gives me this error message:
/usr/bin/env: rvm-shell ree-1.8.7-2012.02#gitorious: No such file or directory
Now, rvm is available, because this works (but doesn't bring the required ruby/gemset):
#!/usr/bin/env rvm-shell
I've tried this as well:
#!/usr/local/rvm/bin/rvm-shell ree-1.8.7-2012.02#gitorious
But this doesn't bring in the environment ("gem", which is only installed inside that gemset, is not available, for example). If I run that on the command line itself, it does open a shell with the proper environment.
So, has anyone done something like this? How can I fix it?

Does this work?
#!/location/of/rvm/folder/rubies/ree-1.8.7-2012.02#gitorious/bin/ruby

Related

Shell Script Ruby Command Not Found

I have a shell script that is running under crontab that kicks back the error: "Ruby: Command not found" for the invocation of a Ruby script. I have no issue running this script when I invoke it through a terminal it is only when running under crontab that I have issues. Anyone have any thoughts?
EDIT: Using RVM, running RHEL.
/path/to/rvm 2.1 do /path/to/script.rb args...
because cron doesn't have your rvm settings, you need to start your script through rvm script (not rvm function) to explicitly choose a Ruby. (Obviously, replace 2.1 with whatever Ruby you want to execute under.)

Ruby 1.9.3 #OSX Lion and Cron

I installed Ruby 1.9.3p125 via this guide (up to point #5): LINK
Now I have this problem: my script works wonderfully from my command line, but if I execute it from Cron it seems to use a default environment and defaults to /usr/bin/ruby instead of mine (~/.rvm/rubies/ruby-1.9.3-p125/bin/ruby). What is the best way to have executed commands - manually or via cron - produce the same results?
PS: It seems to skip processing ~/.bash_login for example, where rvm is loaded into PATH
In your crontab line, you can source the .bash_login before you script is run.
source ~/.bash_login && <your original command here>
That way your script will have everything you have when you run it.
The usual way recommended to do this would be to put the full path to the executable in your crontab. E.g.
crontab should show:
/Users/Poochie/.rvm/rubies/ruby-1.9.3-p125/bin/ruby /full/path/to/script.rb
or whatever the full path is. It's much more robust than trying to get rvm loading, as here an rvm script is modifying your path for you. If you want to set it to whichever is the rvm default ruby (e.g. whatever was set by rvm use x.x.x --default), You can use: /Users/Poochie/.rvm/bin/ruby as the executable instead, e.g.:
/Users/Poochie/.rvm/bin/ruby /full/path/to/script.rb
I actually found this post which helped me a lot: LINK
I managed to run my script as I wanted but the questions is theoretically still open because the issue could still affect cron usage in general.

Ruby 1.9 If I have a shebang pointing to ruby, why doesn't the system see it?

I'm using Ubuntu. I'm trying to run a ruby file todo.rb
I added this shebang as the very first line of the file
#!/usr/bin/env ruby
I go to the directory where the rb file is located and then run todo.rb and get error todo.rb: command not found.
So I went directly to the /usr/bin directory. I found the env command and ran it. The output of the env command displays ruby paths and ruby data:
MY_RUBY_HOME=/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290
PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
GEM_PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global
RUBY_VERSION=ruby-1.9.2-p290
So since, I couldn't make it work, I changed the shebang to point to ruby directly:
#!/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
and I get same command not found error. What's wrong?
You need to first make your script executable:
chmod +x todo.rb
then you need to run it like so:
./todo.rb
You cannot run it by just saying todo.rb, unless you place it in your PATH, in which case you can do so from anywhere.
You're missing the ruby at the end of your env command. Did you mean:
#!/usr/bin/env ruby
You need to tell env what executable you're looking for.
(Question Updated)
Are you executing your code like this? todo.rb ? You either need to provide the full path to your script (/home/you/project/todo.rb) or a relative path (./todo.rb) unless that directory is inside your $PATH.

How to run Ruby scripts in Geektool?

I want to run a Ruby script in Geektool that refreshes every 3 hours (so I set the refresh rate to 10,800 seconds) and the shell command in Geektool has this code in it:
ruby "/file.rb"
The file is located at root for convenience. Problem is, it won't run. I tried different commands, such as:
/Users/userhere/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /file.rb
But it still doesn't work. I don't want it to use my /usr/bin/ruby installation (which, by default, is 1.8.7), I want it to use 1.9.3. So doing: /usr/bin/ruby "/file.rb" won't work for me.
In Terminal, if I run any of those commands they all work (except for the latter, because of dependencies) and my script works fine, but Geektool fails to even execute it. I tried with and without double quotes around the file name, even single quotes don't work.
Any help would be greatly appreciated.
I needed an rvm gemset as well; I created a shell file:
/Users/Dave/.rvm/bin/rvm use 1.9.3-p0 > /dev/null
/Users/Dave/.rvm/rubies/ruby-1.9.3-p0/bin/ruby ~/foo.rb
With that shell file (which happens to reside in a directory off my ~) as the shell command it works fine.
Without the full paths to each command it doesn't work. GeekTool doesn't run your .bash_profile AFAICT. Also not sure that running an rvm Ruby w/o using it would do what you want anyway.

Only one certain user can run Ruby scripts

Whenever I try to run a Ruby script as any user but myself, I get this error:
/usr/bin/env: ruby: No such file or directory
I don't understand why this should be the case. When I installed Ruby, why would it think I only want it for one user?
Here's this if it helps:
$ which ruby
/home/jason/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
RVM defaults to only install for your own user account. However, it looks like they provide instructions for a system-wide installation which will allow access to all users. (However, each user will still need the RVM-specific updates in their shell profiles).
http://rvm.beginrescueend.com/deployment/system-wide/
Maybe someone else will have a better answer but here's something I came up with:
The first line of my script was this:
#!/usr/bin/env ruby
I changed it to this and now other users can run the script:
#!/usr/bin/env /home/jason/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
I think the other users could be missing /home/jason/.rvm/rubies/ruby-1.9.2-p136/bin in their PATH environment variable, so /usr/bin/env can't find ruby. Check their PATH and see if that is the case.

Resources