Score:0

`nohup: failed to run command 'node': No such file or directory` when running script through ssh

fr flag

I have a remote server in which I serve a webserver using node (express) and nginx. I have a ./devops/deploy.sh script which starts the webserver and restarts nginx. Said script works correctly if I manually connect to the instance (ssh <user>@<ip>) and then run

cd my-repo
git pull
./devops/deploy.sh

I can not however set up a script which automates this. I tried with this script

#!/bin/bash
set -euo pipefail

usage() {
  cat << EOF
Usage: ./devops/update-instance.sh <user@ip>
EOF
  exit -1
}

CONNECTION_STRING=${1:-""}

if [ -z $CONNECTION_STRING ]; then
  usage
fi

ssh -t $CONNECTION_STRING << EOF
cd my-repo
git pull
./devops/deploy.sh
EOF

but it gives me nohup: failed to run command 'node': No such file or directory. This is the deploy script which I'm running locally from my machine

#!/bin/bash
set -euo pipefail

lsof -ti:3000 && kill $(lsof -ti:3000)
echo bar
echo $(node -v)
nohup node runner.js &

NGINX_CONFIG_PATH=/etc/nginx/sites-enabled/my-repo
rm -rf $NGINX_CONFIG_PATH
cp devops/nginx-config $NGINX_CONFIG_PATH
nginx -t
nginx -s reload

node runner.js simply starts an express app, so nothing weird there.

What I want to achieve is very similar to this other post https://askubuntu.com/questions/349262/run-a-nohup-command-over-ssh-then-disconnect but no mention of this nohup + node error is given.
After looking at it I realized I'm most probably doing something wrong in how I call my deploy script but I don't really understand what it could be. In the update script I tried doing nohup ./devops/deploy.sh & instead of simply ./devops/deploy.sh but got the same nohup: failed to run command 'node': No such file or directory error.
If I put the content of the ./devops/deploy.sh script inside the ssh ... << EOF <content here> EOF I also get the same nohup: failed to run command 'node': No such file or directory error


Switched the ssh command to

export NVM_DIR="\$HOME/.nvm"
\. "\$NVM_DIR/nvm.sh"
\. "\$NVM_DIR/bash_completion"
cd my-repo
git pull
nohup ./devops/deploy.sh 1>api.stdout 2>api.stderr &
Score:1
bd flag

Running an executable file (in this case, node) by simply giving its name without a path requires that the file lies in one of the directories specified in the environment variable PATH. The exact content of that variable tends to vary between different ways of running a shell, such as logging in interactively or starting a script. Your node executable probably lies in a directory which is listed in PATH when you log in interactively, but not if you run your script.

There are basically two ways to solve this:

  1. Set the PATH variable explicitly at the beginning of your script to include all the directories from which the script intends to run executable files. Do not forget to include /bin and /usr/bin because that's where standard commands reside. So, for example, if your node executable lies in /opt/MyPrettyDevops/bin, you would add the line

    export PATH=/bin:/usr/bin:/opt/MyPrettyDevops/bin
    
  2. Specify the path to the node executable explicitly, by changing the nohup line to

    nohup /opt/MyPrettyDevops/bin/node runner.js &
    
ffigari avatar
fr flag
That was it, I'm managing node via nvm and it seems logging in through ssh would load the bash profile, which would then load nvm and node. Had to manually load nvm and node in the script. I then had to follow the stdout / stdin suggestions for the ssh call to finish.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.