rvm-shell command stops executing other commands - ruby

I have written a shell script where i wrote one rvm-shell command. The issue is any line after this command will not get executed. The script is as follows:
#!/bin/bash
set x
rvm-shell ruby-1.9.3-p448#global //after this line not anything get executed
echo $? //not get executed
clear //not get executed
rails s //not get executed which i want to run

rvm-shell ruby-1.9.3-p448#global
without any parameters is equivalent to running
rvm ruby-1.9.3-p448#global do bash
which will enter shell session and wait for your input.
what you need is:
rvm use ruby-1.9.3-p448#global
or:
source "$( rvm ruby-1.9.3-p448#global do rvm env --path )"
debugging source:
env_file="$( rvm ruby-1.9.3-p448#global do rvm env --path )"
echo "env_file:$env_file:"
source "$env_file"
or use this script:
#!/usr/bin/env rvm-shell ruby-1.9.3-p448#global
rails s

Related

Automatic RVM installation

I am trying to have RVM and ruby installed in an Ubuntu 12 virtual machine without human interaction apart from the password prompts.
I created a shell script to do this that works pretty fine until I need to use RVM itself.
I am using multi-user installation.
#!/bin/bash -l
mainUser=`whoami`
echo "Installing as '${mainUser}'"
echo "Installing git..."
sudo -S apt-get install --yes curl git-core
echo "Installing RVM..."
\curl -L https://get.rvm.io | sudo bash -s stable
echo "Adding ${mainuser} to RVM group..."
sudo adduser $mainUser rvm
newgrp rvm
From here things get weird.. I need to load dvm as a source. I want both my script to have this source and my user's bash_profile / bashrc. Anyway.. I know how to do it manually, but I can't have this done from the script. This is the last code I tried:
. "/usr/local/rvm/bin/rvm"
rvm use ruby-head
rubyVersion=`rvm list | awk '/ruby-head/{print x;print};{x=$0}' | sed -n '/ruby-head/{g;1!p;};h' | awk -F ' ' '{print $1}'`
rubyTest=${rubyVersion}#test
rvm use $rubyTest --create --default
The error I get is this:
test.sh: 7: /usr/local/rvm/bin/rvm: Syntax error: "(" unexpected (expecting "fi")
If I simply try to use the full path, like this:
rvm=/usr/local/rvm/bin/rvm
$rvm use ruby-head
rubyVersion=`$rvm list | awk '/ruby-head/{print x;print};{x=$0}' | sed -n '/ruby-head/{g;1!p;};h' | awk -F ' ' '{print $1}'`
rubyTest =${rubyVersion}#test
$rvm use $rubyTest --create --default
I get this error instead:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
I am clueless. Why can't I use /usr/local/rvm/bin/rvm?
Is there a way to execute source /etc/profile.d/rvm.sh for this user from the script?
I am not so good at shell script and Linux, so I appreciate any references and examples you could give.
Thanks!
UPDATE
I also tried:
source "/usr/local/rvm/scripts/rvm"
... and all it's variants. Same error: "RVM is not a function".
rvm is actually implemented as a shell function rather than an executable, which is why you can't just call /usr/local/rvm/bin/rvm itself.
Quoting you, "Is there a way to execute source /etc/profile.d/rvm.sh for this user from the script?"
Have you tried doing that? I had a somewhat similar install once where it didn't work properly from crontab (they have instructions on the site for that scenario, but we couldn't make them work), and I had to do almost exactly that -- source part of the profile.d for rvm.
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Above error is generated when rvm is not running and hence your terminal is not able to recognize it, as it tries to run it as a system command.
You may want to try this to run rvm through your shell script before calling rvm methods:
source ~/.rvm/scripts/rvm
I found out what is causing the issue.
I realised before that I would get errors in the lines with conditions in the script files, so I came across this page:
https://superuser.com/questions/552016/bash-script-not-found
As it happens, I was executing the script with the following command:
sh script.sh
Which means I was getting Dash instead of Bash.
To fix the issue I changed my code to have this:
PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm" # Load RVM function
And then I executed like this:
bash script.sh
And voilĂ ... RVM works again!

Initialize rbenv and run ruby script from shell script

Initialize rbenv and run ruby script from shell script
I want svnserve to run pre-commit hook, written on ruby. As the svnserve is run as root user, it knows nothing about user rbenv installation.
I set a soft link /usr/bin/ruby -> /home/admin/.rbenv/shims/ruby .
As result, when i try
#!/usr/bin/ruby
puts "Pre-commit hook!"
It shows error:
Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.
When i run manually on Server:
admin $ sudo ./pre-commit
/usr/bin/ruby: line 4: exec: rbenv: not found
So, i suppose, that rbenv initialization is needed, but how?
In hooks path:
pre-commit:
#!/bin/bash
export HOME=/home/user
if [ -d $HOME/.rbenv ]; then
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
fi
$0.rb $*
pre-commit.rb:
#!/usr/bin/env ruby
ARGV.each_with_index { |arg, index| puts "Index #{index}: Argument #{arg}" }
you should initialize rbenv before using it.
/path/to/user/home/.rbenv/bin/rbenv init
then set ruby version globally:
rbenv global DESIRED-RUBY-VERSION
or localy:
rbenv local DESIRED-RUBY-VERSION
or per shell:
rbenv shell DESIRED-RUBY-VERSION
though not sure shell setting will work without a tty.
so you could create a shell script, pre-commit.sh and register it as a svn hook.
inside it initialize rbenv and call your pre-commit.rb file

Ruby RVM under another user from Bash script

What is the best way to launch ruby script under another user via su command and from another script?
I have a bash script with the following command for launching unicorn:
sudo -u unicornuser sh -l -c "bundle exec unicorn_rails -E production -D"
This script is an init.d script and it being execute when system starts and manually also.
The problem is the default ruby on system is 1.8, other rubyes (1.9) is working under RVM. I need to modify above script that it can execute ruby with RVM (non system wide)
Now I've migrated for this notation:
su -l -c "rvm use ruby-1.9.3-p125 && bundle exec unicorn_rails -E production -D" unicornuser
this worked for me, but I guess there must be a better way to do this.
You should use wrappers:
rvm wrapper ruby-1.9.3-p125 ext_1.9.3 bundle
This will create $rvm_path/bin/ext_1.9.3_bundle, so now you can use:
/full/path/to/rvm/bin/ext_1.9.3_bundle exec unicorn_rails -E production -D
replace /full/path/to/rvm with output from echo $rvm_path

Why does ruby script stop when trying to start unicorn_rails as daemon?

I'm trying to start unicorn_rails in a ruby script, and after executing many commands in the script, when the script gets to the following line
%x[bash -ic "bash <(. ~/.bashrc); cd /home/www-data/rails_app; bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -D"]
the script stops, generating the following output
[1]+ Stopped ./setup_rails.rb
and returns to the Linux prompt. If I type "fg", the script finishes running, the line where the script had stopped gets executed and unicorn gets started as a daemon.
If I run the line in a separate script, the script completes without stopping.
UPDATE_1 -
I source .bashrc because earlier in the script I install rvm and to get it to run with the correct environment I have the following:
%x[echo "[[ -s \"$HOME/.rvm/scripts/rvm\" ]] && source \"$HOME/.rvm/scripts/rvm\"" >> .bashrc]
%x[bash -ic "bash <(. ~/.bashrc); rvm install ruby-1.9.2-p290; rvm 1.9.2-p290 --default;"]
So if I want to run correct version of rvm, ruby and bundle I need to source .bashrc
end UPDATE_1
Does anyone have any idea what could cause a ruby script to halt as if control-Z was pressed?
Not sure why it's stopping, but my general rule of thumb is to never source my .bashrc in a script -- that might be the source of your problem right there, but I can't be sure without seeing what's in it. You should be able to change your script to something like:
$ vi setup_rails.sh
#!/usr/bin/bash
# EDIT from comments below
# expanding from a one liner to a better script...
$RVM_PATH=$HOME/.rvm/scripts
# install 1.9.2-p290 unless it's installed
$RVM_PATH/rvm info 1.9.2-p290 2&>1 >/dev/null || $RVM_SH install 1.9.2-p290
# run startup command inside rvm shell
$RVM_PATH/rvm-shell 1.9.2-p290 -c "cd /home/www-data/rails_app && bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -D"
This should give you the same result.

Problem deploying Ruby+RVM and daemontools

I am using daemontools in production to keep the services alive and want to run a Ruby server, not Rails, and without RVM it works well but with RVM I have some issues.
My goal is to start a process by root, make it drop root rights to get another user rights and then spawn a Ruby process with RVM and a specified Ruby version.
Here is the run script I was using until now:
#!/bin/sh
exec 2>&1
cd /app/src
. /usr/local/rvm/scripts/rvm
rvm use 1.9.1-p378
exec setuidgid app_user ruby main.rb
This script works but setuidgid has a major problem: the application will be run by user <x> and group <x> and only this group. If the user is in other groups the process will not have their rights.
So it led me to another approach:
#!/bin/sh
exec 2>&1
cd /app
exec sudo -u app_user rvm 1.9.1-p378 exec ruby main.rb
This one works fine except it is the RVM process which is spawned by daemontools and it does not react when it receives a SIGTERM which is not really nice. Basically it means the service cannot be restarted by hand, which is not good.
I found the answer but looking at the rvmsudo script installed with rvm, here is a working run script:
#!/bin/sh
# redirect stderr to stdout
exec 2>&1
cd /app
# load rvm
. /usr/local/rvm/scripts/rvm
# select ruby version for this application
rvm use 1.9.1
# # depending on your configuration you may need to provide the absolute path to rvm, like that:
# /usr/local/bin/rvm use 1.9.1
# build the exec command line preserving the rvm environment
command="exec sudo -u app_user /usr/bin/env PATH='$PATH'"
[[ -n "${GEM_HOME:-}" ]] && command="${command} GEM_HOME='$GEM_HOME' "
[[ -n "${GEM_PATH:-}" ]] && command="${command} GEM_PATH='$GEM_PATH' "
# this is where your real command line goes
command="${command} ruby main.rb"
# run the application
eval "${command}"

Resources