Shell Script Ruby Command Not Found - ruby

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.)

Related

How to remotely execute a bash script in pycharm?

I own the licensed version of Pycharm. I have installed BashSupport plugin as well.
However, I am not sure what steps are required to remotely test and execute a bash script on RHEL server.
I am able to configure a remote interpreter but the interpreter path is /usr/bin/python.
In my logic you need to change interpreter path to /bin/bash instead of /usr/bin/python or i don't understand what you are trying to do..
I've met the save problem today.
If I use the default pycharm bash configuration, I'll get the following error:
C:\windows\system32\bash.exe C:/git/NewProject/run.sh
/bin/bash: C:/git/NewProject/run.sh: No such file or directory
Process finished with exit code 127
So, I change the default Windows Script path to Linux absolute Script path as following:
Run successfully.
You gotta check the path on server, If there is alias set, You can check alias
$ which python
$ whence python (Some version)
$ ls -l `which python`

Install Homebrew from within a ruby script

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)

RVM on Ruby Scripts

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

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.

Cannot execute RVM shell commands in ruby

Long time lurker, first time poster!
Goal
My ultimate goal is to make a Rake setup script to setup my rvm environment stuff (I need to dynamically create gemsets, install gems to those gemsets, and run ruby scripts within those gemsets).
Problem
I need to setup rvm in the shell that I'm executing rvm commands in. The basic idea is to source the rvm scripts as outlined here.
The problem arises when I try and source the rvm script when executing a shell command within ruby. Its well documented that rvm only supports bash, but ruby doesn't seem to be using bash when executing shell commands.
What I've Tried
I've tried all the methods to execute shell commands listed here to no avail. I'll use the 'exec' method below for simplicity.
It seems that although ruby thinks its using the bash shell to execute these commands ... it is not. Observe!
exec 'echo $SHELL'
=> /bin/bash
But
exec 'source ~/.rvm/scripts/rvm; type rvm | head -1;'
=> sh: source: not found
=> rvm is ~/.rvm/bin/rvm
Which tells me that ruby is really using /bin/sh not /bin/bash (that output should return rvm is a function). I even went so far as to print the ruby env stuff, and ENV[SHELL] is '/bin/bash'
'Brute Force' Solution
I do have a workaround, but its really kludgy (this would necessitate that I 'AND' all of the commands together):
exec 'echo \'source ~/.rvm/scripts/rvm && type rvm | head -1\' | /bin/bash'
I'd like to avoid using shell scripts if possible -- it seems reasonable that I can accomplish this within ruby.
As it happens, RVM actually exposes a Ruby API that's included by default. Add $HOME/.rvm/lib to your $LOAD_PATH; you can now use require 'rvm'.
As far as I can tell, the main documentation for this is in the source files themselves (a summary is in rvm.rb).
Now you can write Ruby scripts that manipulate RVM, like this:
require 'rvm'
env = RVM.current
env.gemset.create('newgemset')
And so on.
Call bash with the -c parameter:
command = 'source ~/.rvm/scripts/rvm; type rvm | head -1'
exec "bash -c #{command.inspect}"

Resources