I'm trying to run a simple command from an init.d script and I'm troubleshooting why the command doesn't work when I run it from my init.d script but works when I run it as my user.

Can someone explain why when I run nvm --version as my logged in user it shows a value, but when I su to my user it does not know what nvm is? This is ultimately the root cause of my init.d script not working I believe.

$ whoami
someuser@node-server1:/var/www/dev$ nvm --version
someuser@node-server1:/var/www/dev$ su - someuser -c "nvm --version"
-su: nvm: command not found

I noticed this in my .bashrc:

export NVM_DIR="/home/someuser/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

Do I somehow need to run this as a command in my init.d script or when I su to a user?


From the NVM documentation:

To activate nvm, you need to source it from your shell: source ~/.nvm/nvm.sh

So, yes, in order to be able to use nvm, you must first "activate" it before invoking it.

| improve this answer | |

This is because the 'someuser' user's $PATH does not have the path where the nvm binary is located. To be able to use the command nvm you need to add the location of nvm binary to that user's path.

Do this:

Run which nvm as the user for whom nvm command is currently availble. If will show you where the nvm command is located. For example, if which nvm gives you /usr/local/nvm/bin/nvm, then you have to add /usr/local/nvm/bin to the path of the user where nvm is not currently available. For that, add the following line in the new user's .bashrc.

export PATH=$PATH:/usr/local/nvm/bin

Once, its done, log out, log back in, or source the .bashrc file and the nvm command will be available for the new user.

For example: If user 'abc' does not have nvm available, add the export PATH... line to /home/abc/.bashrc

| improve this answer | |
  • I can use the command $ nvm use default, but $ which nvm doesn't show anything: $ nvm use default Now using node v0.11.14 myuser@node-server1:~$ which nvm – Catfish Dec 11 '14 at 14:34

Where is located the nvm program? To find this out, you can type whereis nvm. It looks like this directory is added to the path somewhere inside your .bashrc file.

What you have to know is that "su -c" runs a non-interactive shell. You can check this by looking into $- variable content :

# echo $-
# su -c 'echo $-' - $USER
Mot de passe : 

The 'i' means the shell is currently interactive. Well, in ubuntu, you can notice that your .bashrc file starts with those few lines:

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

This means that this file isn't sourced if the $- variable doesn't contain an 'i', i.e while in an non-interactive shell.

You have two possible workarounds:

1- Run nvm with sudo command instead of su:

# sudo -u $USER echo $-

Note that because of sudo's behaviour, you mustn't use quotes.

2- Use nvm's full path to the binary and source the required file:

# su -c 'source $HOME/.nvm/nvm.sh; /path/to/nvmdir/nvm --version' - $USER

Using nvm instead of its full path /path/to/nvmdir/nvm may be sufficient if $HOME/.nvm/nvm.sh is the one updating your $PATH.

| improve this answer | |

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.