SSH执行远程script, command not found.

问题:通过PUTTY进入,bash -s < xxx.sh可以正常运行。
远程SSH进入,并执行bash -s < xxx.sh 报错npm command not found.

# xxx.sh
npm run start

原因: EXPORT PATH配在/etc/profile里。
/etc/profile 是账户的配置文件,login shell (如putty) 会读这里加载环境变量,所以command可以正常执行。
而通过命令行SSH并执行远程script,创建的是non-login shell,此时不会读/etc/profile。

ssh -p ${(remotePort )} -i ${privateKeyPath} ${sshServer} 'bash -s' < ${script on remote machine}

/etc/bashrc 是bash shell的配置文件,通过远程ssh执行bash,需要在这里同样配置环境变量。

# /etc/bashrc
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/node/bin