9

I have a very strange configuration issue.

When logging to machine (SSH session) and executing the following commands using interactive shell, they succeed:

node --version
npm --v

However, when I execute the following through script from remote machine:

#!/bin/bash

#Script input arguments
user=${1} 
server=${2} 

#Tell the shell to quote your variables to be eval-safe!
printf -v user_q '%q' "$user"
printf -v server_q '%q' "$server"
#

# Script variables
address="$user_q"@"$server_q"
#

function run {
    ssh "$address" /bin/bash "$@"
}


run << SSHCONNECTION

node --version
npm --v

 exit
SSHCONNECTION

I get the following output:

/bin/bash: line 2: node: command not found

/bin/bash: line 3: npm: command not found

I am conscious that the issue may be the in .bash_profile file configuration

export PATH="/usr/local/bin/npm:/usr/local/bin/node:/usr/local/bin:$PATH"

Also, here is result of npm config get prefix:

/usr/local

Can you please suggest what I am doing wrong?

Community
  • 1
  • 1
Alex
  • 937
  • 3
  • 20
  • 44
  • 8
    What do you get when you run `which npm` and `which node` on the machine? – rakeshdas Apr 18 '17 at 21:45
  • @rakeshdas `/usr/local/bin/node` and `/usr/local/bin/npm` – Alex Apr 18 '17 at 21:47
  • 1
    In your script, what if you just used the directory you got back to run the command: `/usr/local/bin/node --version` ? – rakeshdas Apr 19 '17 at 02:55
  • I get the output using `/usr/local/bin/node --version` , but don't get the output for `/usr/local/bin/npm --v` : env: node: No such file or directory – Alex Apr 19 '17 at 15:44
  • Try running this on the remote machine: `sudo ln -s "$(which node)" /usr/local/bin/node` – rakeshdas Apr 19 '17 at 17:06
  • @rakeshdas /usr/local/bin/node already exists on the box, and `which node` resolves to it. Ran the command to create the link and got the following output: `ln: /usr/local/bin/node: File exists` – Alex Apr 19 '17 at 17:53

1 Answers1

0

I believe what happened is due to the fact .bash_profile is only executed when bash is invoked for an interactive shell, more info about this here: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files

When invoking and passing the script directly to /bin/bash, it will behave as a non-interactive shell which the default behaviour to not read the .bash_profile.

I would suggest setting you PATH variable in /etc/environment so it will take affect for all users and all manners of scripting.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115