大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 instan…

问题原文

原帖链接 · ffigari · CC BY-SA 4.0

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 &

已采纳解答

原帖链接 · Tilman Schmidt · CC BY-SA 4.0

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 &
    

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-05-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。