Bash not loading node in remote ssh command

Sorry if the topic is vague, but I tried to describe my problem to the best of my ability. I have a raspberry pi that I want to deploy using code. The rsyncing file works fine, but when I have to restart my application using pm2 , my problem arises.

I installed node and pm2 using node NVM version manager.

 ssh pi@server.com 'source /home/pi/.bashrc; cd project; pm2 restart app.js -x -- --prod'0 min 3 sec bash: pm2: command not found 

I even added:

shopt -s expand_aliases at the bottom of my bashrc , but that doesn't help.

How can I restart my application after I deployed? Thanks in advance for the advice of the sage and the best wisdom!

EDIT 1 : My.bashrc http://pastie.org/10529200 My $ PATH: /home/pi/.nvm/versions/node/v4.2.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

EDIT 2 : I added /home/pi/.nvm/versions/node/v4.2.0/bin/pm2 , which is the full path to pm2, and now I get the following error: /usr/bin/env: node: No such file or directory

It seems that even if I provide the full path, node fails.

+7
source share
6 answers

I think the problem is misunderstanding that the shell executing the node has a complete environment such as an interactive ssh session. Most likely, this is not so.

When an SSH session spawns a shell, it goes through many cycles to create an environment suitable for interactive interaction. Things like inheriting the login process, reading /etc/profile , reading ~/.profile . But in cases where your bash launch is direct, this is not always guaranteed. In fact, $PATH can be completely empty.

When /usr/bin/env node is executed, it searches for a node in your $PATH , which in the non-interactive shell can be anything or empty.

On most systems, by default PATH=/bin:/usr/bin usually /usr/local/bin not included in the default environment.

You can try to force login using ssh to use ssh โ€ฆ '/bin/bash -l -c "โ€ฆ"' .

You can also write a specialized script on a server that knows how the environment should work outside the interactive shell:

 #!/bin/bash # Example shell script; filename: /usr/local/bin/my_script.sh export PATH=$PATH:/usr/local/bin export NODE_PATH=/usr/local/share/node export USER=myuser export HOME=/home/myuser source $HOME/.nvm/nvm.sh cd /usr/bin/share/my_script nvm use 0.12 /usr/bin/env node ./script_name.js 

Then call it via ssh: ssh โ€ฆ '/usr/local/bin/my_script.sh' .

Besides these ideas, I donโ€™t see how to help further.

+11
source

As Sukima said, the likelihood that this is due to an environmental problem - SSH'ing to the server does not create a complete environment. However, you can work around this problem by simply calling / etc / profile at the beginning of your command using. statement (which matches the "source" command):

 ssh pi@server.com '. /etc/profile ; cd project; pm2 restart app.js -x -- --prod' 

/ etc / profile must be configured to invoke the .bashrc of the corresponding user, so I deleted this part. I used to do this quite a lot for a quick authentication script at a previous workstation. I donโ€™t know if this will be considered an unpleasant hack for a more permanent script, but it certainly works and will require minimal modification of the existing script if this is a problem.

+6
source

Try:

 ssh pi@server.com 'bash -l -c "source /home/pi/.bashrc; cd project; pm2 restart app.js -x -- --prod"' 
+4
source

What worked for me is to add to my .bash_profile :

 if [ -f ~/.bashrc ]; then . ~/.bashrc fi 

Source: fooobar.com/questions/24758 / ...

+1
source

You must include some environment values โ€‹โ€‹using the "source" or the dot command ".". Here is an example.

 ssh pi@server.com '. /home/pi/.nvm.nvm.sh; cd project; pm2 restart app.js -x -- --prod' 
0
source

I ran into the same issue at Jenkins.

The following lines were at the bottom of the .bashrc file, I just put the top of the .bashrc file

 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 
0
source

All Articles