0

My goal is to execute two commands in a specific folder as ubuntu from outside of it's lxc container.

I've tried a couple of things but I figured this example is the closest I have to working.

If I run

root@host$  lxc exec my-containter -- sudo --login --user ubuntu eval "cd /home/ubuntu/mydir && pwd && whoami && env && npm install && echo done"

I get an npm install error that can't find some module, but it looks like I'm the right user

However if I manually do it as two steps it does work... but I'm trying to put this in a bash script, so that I can keep doing operations on the host, so I think I need it as one.

root@host$  lxc exec my-containter -- sudo --login --user ubuntu 
ubuntu@my-container$  eval "cd /home/ubuntu/mydir && pwd && whoami && env && npm install && echo done";

I discovered that my PATH environment variable is different in these two situations, the one that is failing is missing a specific path for nvm/npm. I tried exporting it during the eval command, but it seems like the resources available to me have already been found? What could I do to make the PATH variable populate the same way in the single line scenario?

PATH from 1-line (non-interactive)

PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin:/snap/bin

PATH from 2-lines (interactive)

PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/.nvm/versions/node/v8.9.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin

I've also noticed this nvm code at the bottom on my .bashrc file. From what I've read it sounds like the .bashrc file only gets executed in interactive mode.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
fei0x
  • 4,259
  • 4
  • 17
  • 37
  • Try `lxc exec my-containter -- sudo --login --user ubuntu sh -lc "cd /home/ubuntu/mydir && pwd && whoami && npm install && echo done"`. If that doesn't work instead of `sh` try with `bash` – Tarun Lalwani Jun 21 '19 at 17:32
  • my PATH variable is still different testing that with sh and bash. I test the PATH contents by checking with "&& env" I assume the PATH (and other variables) are set with NVM, but I'm not sure how they're set. – fei0x Jun 24 '19 at 14:40
  • Can you show both the `env` results? The `-l` flag I had added make sure that the profile is also loaded – Tarun Lalwani Jun 24 '19 at 14:46
  • I've noticed NVM has lines at the bottom of the bashrc file, and it sounds like this file doesn't get run when running in non-interactive mode. I'll update the main question with that and the PATH – fei0x Jun 24 '19 at 15:02
  • I think I figured it out. Please use `bash -ilc` – Tarun Lalwani Jun 24 '19 at 15:31

1 Answers1

2

The below command should do the job for you

lxc exec my-containter -- sudo --login --user ubuntu bash -ilc "cd /home/ubuntu/mydir && pwd && whoami && npm install && echo done"

The .bashrc file has below at the top

case $- in
    *i*) ;;
      *) return;;

This code prevents the rest of the part of .bashrc to be executed in case of a non-interactive bash. So to make it interactive you should add the -i flag

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • perfect. that did the trick, much better than the path I was going down: https://github.com/nvm-sh/nvm/issues/1718 – fei0x Jun 24 '19 at 16:19