2

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

  • Where is the rvm function defined? – terdon Mar 1 '17 at 13:27
  • which rvm gives /home/build/.rvm/bin/rvm I think it was already installed on the VM but my installation script reinstalls it anyway. Using instructions here rvm.io – Paul D'Ambra Mar 1 '17 at 13:30
  • Well, your type output shows it is a function, so it must be defined in a file you're sourcing somewhere. Presumably one of the profile files like ~/.profile or /etc/profile. What problem are you trying to solve here? The RVM is not a function. . . or the login shell issue? Are they the same problem? Two separate ones? What needs to be run in a login shell? The agent.sh script? – terdon Mar 1 '17 at 13:35
  • @terdon does that clarification help? – Paul D'Ambra Mar 1 '17 at 13:40
  • Yes, thanks. The difference between login and non-login shells is basically just what files are sourced when the shell is started. I am guessing you could solve this by defining rvm function in one of the file sourced by non-login shells instead of where you currently have it. So, where is the rvm function defined at the moment? Is it in ~/.profile? /etc/profile? ~/.bash_profile? – terdon Mar 1 '17 at 13:41
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.

  • hmmm, I had to change it to /bin/bash -l before it picked it up in the agent.sh script. Which if I add the head foo call to it now prints out 'rvm is a function' frustratingly teamcity run scripts now report rvm is /home/build/.rvm/bin/rvm which this rvm.io/workflow/scripting says means that in the context of the teamcity script it is a binary... – Paul D'Ambra Mar 1 '17 at 14:39
  • @PaulD'Ambra that's odd since dash is supposed to have an -l option. Could it be that you were using ~/.bash_profile instead of ~/.profile? – terdon Mar 1 '17 at 14:40
  • hmmm, I can add a shebang to the teamcity scripts and if I add -l to that it works... – Paul D'Ambra Mar 1 '17 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.