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.
Related
The crontab -l below doesn't seem to run. The script run by hand runs fine. Here is the error i'm seeing
Dec 3 20:12:01 dahlia /USR/SBIN/CRON[13912]: (gigawatt) CMD (/bin/sh -c "(export PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)")
Dec 3 20:12:01 dahlia /USR/SBIN/CRON[13910]: (CRON) error (grandchild #13912 failed with exit status 1)
And here is the crontab:
* * * * * /bin/sh -c "(export PATH=/usr/bin:/bin; /home/gigawatt/drbronnersbot/drbronnersbot.rb)"
Permissions are fully open, its executable, i put the env path at the beginning of the file, still no dice.
Edit: I just noticed the user answered his own post, but I'll leave this up in case someone stumbles across here with a similar issue. I've found it's helpful for some.
I've run into this and found that this is the solution for Ruby scripts.
Ruby needs to be executed in a particular environment. RVM handles this by sourcing a ruby environment file for you particular version of ruby that sets all the required environment variables. For example if you have ruby 1.9.3 patch 448, you can look at the environment file that is sourced:
cat /usr/local/rvm/environments/ruby-1.9.3-p484
export PATH="/usr/local/rvm/gems/ruby-1.9.3-p484/bin:/usr/local/rvm/gems/ruby-1.9.3-p484#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p484/bin:$PATH"
export GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p484'
export GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p484:/usr/local/rvm/gems/ruby-1.9.3-p484#global'
export IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p484/.irbrc'
unset MAGLEV_HOME
unset RBXOPT
(Note: My installation of rvm was under /usr/local/.. but yours may be elsewhere. Use which ruby to figure out where your ruby is installed)
Here you can see that it's setting the PATH and some other important environment variables. When you type rvm use ruby-1.9.3-p448, rvm actually sources this file in the background.
Cron executions are non-interactive sessions, which means they have no "live" user logged-in in front of a session. When you do it manually and run an interactive session, all this is taken care of for you, but for a non-interactive session, it doesn't know what the shell is or where to find the environment path. Maybe someone with more knowledge can provide a technical explanation as to why.
Anyways, to get around that, add this to the top of your crontab:
SHELL=/bin/bash
BASH_ENV=/home/gigawatt/.bashrc
* * * * * /home/gigawatt/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/gigawatt/drbronnersbot/drbronnersbot.rb
This is telling the non-interactive cron user which shell to use and then telling it to source the .bashrc file. What's in the .bashrc file? Good question, you should add this line -
source /usr/local/rvm/environments/ruby-1.9.3-p484
(Once again, replace with your own ruby path)
Basically you are manually sourcing the environment file that rvm would have sourced for you. It's a way of getting cron to use a particular gem environment or gemset.
It should work with those two changes.
Edit2: On Ubuntu and similar systems, the default .bashrc often contains something like
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
As the comment implies, the file wont run in a non-interactive/cron session. Anything you add under that line won't be executed.
In that case, either put your source command above that line or just use a different file all together, like ~/.cronrc for example.
I've gotten it working using
* * * * * /bin/bash -l -c 'ruby my-ruby-file.rb'
This is quite old but thought I'd comment anyway. RVM creates a wrapper for each version of ruby you install.
run this command replace 2.5.1 with your ruby version
rvm env --path 2.5.1
this gives you the path to the environment mine is
/home/username/.rvm/environments/2.5.1
you want just the rvm path
/home/username/.rvm/
take a look at the wrappers directory there should be a wrapper for each version and gemset you have installed. Test your script with the wrapper
/home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb
or if you have a specific gemset
/home/username/.rvm/wrappers/ruby-2.5.1#gemset/ruby /home/username/scripts/script.rb
Use this command directly in your crontab
* * * * * /home/username/.rvm/wrappers/ruby-2.5.1/ruby /home/username/scripts/script.rb
Solved it, i was calling two files in my code and it couldn't find those files. I editing the .rb file to include the full path and it works perfectly now. Thanks everyone!
Have you tried:
* * * * * /home/gigawatt/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/gigawatt/drbronnersbot/drbronnersbot.rb
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
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.
Is it possible while using the Ruby version manager to run scripts not from the console but using other ways — at system startup or by a keyboard shortcut for example?
RVM installs a command rvm-shell. You can use rvm-shell, pass it whatever you would pass rvm use, then you can execute a shell command.
rvm-shell will set your environment for that shell script, or you can use rvm-shell on one line, and have it execute the parameter as a shell command.
For example:
rvm-shell rbx-2.0 -c 'which ruby'
Which should equal your rbx ruby.
It'd help to know the system, but the answer is yes, though you'd need to either know which is the current directory, or set the system ruby to be the one you needed for these scripts (especially startup). You might also need to experiment, as it would depend at which point in the startup you needed the scripts to run, but you can probably get more answers on that from the irc rvm channel.
rvm default do /path/to/ruby/script
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.