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.
Related
I've created a ruby script that sets up a new mac.
Among other things it creates a .bash_profile, .gitconfig and configures various system settings such as displaying the full POSIX path as the Finder window title (super useful).
Mostly I'm running commands in backticks such as `defaults write com.apple.finder _FXShowPosixPathInTitle -bool true` the aforementioned full POSIX path as the Finder window title trick.
All this works just fine.
What I want to do is have this ruby script run the Homebrew installer too. The bash command for this is :
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
However this doesn't work when called using backticks.
So my question is how do I run another ruby script (which the Homebrew installer is) from within a ruby script?
And more specifically how would I kick off the web based interactive Homebrew installer (well you have to press return at least once) from within a ruby script and for it's output to show in the terminal?
I know that I could rewrite this all as bash script but I'd really rather keep it all within ruby.
Let's decompose what $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" actually does:
download, via curl, the homebrew install ruby file. Since the command is surrounded by $(), it executes the command and passes the output to ruby.
execute the script via Ruby. The -e flag instructs Ruby to execute the script from the command line instead of loading a specified file.
Since we know that it's a ruby script, we can just do the following:
using Net::HTTP or some other ruby library, download, the homebrew install file.
eval() or otherwise execute the homebrew ruby script.
Of course, eval() is dangerous, especially with untrusted input, but you're already essentially running eval on the script anyways with the install command provided.
In script form that would be:
require 'net/http'
homebrew_uri = URI('https://raw.githubusercontent.com/Homebrew/install/master/install')
homebrew_script = Net::HTTP.get(homebrew_uri)
eval(homebrew_script)
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 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.
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
I'm using Ubuntu 9.04 x64 and,
I have a file startup.rb in which I call sudo bash, so that I always have a root console to do administrative tasks without typing password after every 15 minutes or so.
This script is called by another script Startup.rb, and content of both files are like this -
File ~/Startup.rb
#!/usr/bin/ruby
system "gnome-terminal --maximize -x ruby ~/startup.rb"
File ~/startup.rb
#!/usr/bin/ruby
`sudo some-repetitive-administrative-task`
....
....
`sudo bash` #Not using `sudo -i`, since that makes `pwd` -> /root
I have included the ~/Startup.rb file in Startup Applications list.
Now the problem is that, in the terminal of sudo bash, if I type something and expect some output, I don't get any. So if I write echo hello world, I don't get any output. Which leads me to believe that the standard output (stdout) of the sudo bash command is not the console.
So, I want to know why is this happening? How may I know the current stdout path? Or how can I restore stdout back to my current console?
-- thanks
You're using an inapproriate method to run system commands from Ruby. Try this instead:
#!/usr/bin/ruby
system 'bash'
The syntax you're using (with the backticks) captures the standard output of the command and returns it in a string. That's why you don't see it on the terminal.
Here's a nice review of the different ways to run commands from Ruby: 6 Ways to Run Shell Commands in Ruby.
If you are only interested in an easier way to run administrative tasks as root, there might be a few other things you might consider.
$sudo -s
This will give you a shell in which you can submit commands as your sudo'd self (assuming that sudo has been setup so that you can run a shell via sudo).
Another thing you can do, though not always recommended or considered good form in Ubuntu is to create a root account:
$sudo passwd root
then you can login as root and administer things that way.
I know this doesn't answer your specific question about ruby, but I hope you find it helpful.