Ubuntu: Using RVM from within a service context



Question:

I have a user set up on Ubuntu 14.

If I check when logged in as the user

build@linux-build-agent-1:~$ type rvm | head -1  rvm is a function  

However, this machine is going to be a teamcity agent...

When a job is run in the context of the teamcity service we see

[13:19:11][Step 1/1] RVM is not a function, selecting rubies with 'rvm use ...' will not work.  [13:19:11][Step 1/1]   [13:19:11][Step 1/1] You need to change your terminal emulator preferences to allow login shell.  [13:19:11][Step 1/1] Sometimes it is required to use `/bin/bash --login` as the command.  [13:19:11][Step 1/1] Please visit https://rvm.io/integration/gnome-terminal/ for an example.  

I've tried several commands trying to force the service to run in a login shell

su - build -c 'source "$HOME/.rvm/scripts/rvm" && /home/build/buildAgent/bin/agent.sh start'      sudo -u build -i bash -lc '/home/build/buildAgent/bin/agent.sh start'  su - build -c '/home/build/buildAgent/bin/agent.sh start'  

within the /etc/init.d/ script with no effect.

If I run source "$HOME/.rvm/scripts/rvm at the head of the script in teamcity then everything is OK but there are multiple users and I can't ask them all to remember to do that.

Is this how I've installed RVM or..?


To clarify:

1) when I'm logged in as the user over SSH rvm is installed correctly and functions as expected. As shown by type rvm | head -1 => 'rvm is a function' 2) when a service logs in as the user rvm is not working correctly.

I want rvm to work when running in the context of the service so that users don't have to explicitly source "$HOME/.rvm/scripts/rvm in their build scripts

Typically this issue is caused by not running as a login shell. So I need to know either how to run a service as a login shell. Or how to fangle rvm so that it can work outside of the login shell context


Solution:1

When you run su - build -c foo.sh, you are starting a login shell, which then launches foo.sh. The script foo.sh, however, is running in its own subshell (that's how scripts work) and the type of that shell depends on the shebang line of the script. So, su - -c foo.sh is not making foo.sh run in a login shell, it is just running a login shell which then launches foo.sh.

So, edit /home/build/buildAgent/bin/agent.sh and change this:

#!/bin/sh  

to this:

#!/bin/sh -l  

That will make it run in a login shell and should cause it to read the /etc/profile, and ~/.profile files and bring in your function. Please note that ~/.bash_profile is not relevant here. This is a file that is only read by bash, and not sh which, on Ubuntu, is actually dash.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »