Search code examples
rubybashrvmsudosu

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.


Solution

  • 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