为什么 ssh 不能在 shell 中运行 rbenv?

为什么 ssh 不能在 shell 中运行 rbenv?

我正在尝试在远程服务器上运行 bash 脚本以通过 rbenv 安装 ruby​​。

#!/bin/bash
# ssh [email protected] 'bash -s' < configure.sh

echo "ruby 3.2.1"
rbenv install 3.2.1 --verbose
rbenv global 3.2.1

远程服务器上rbenv已为用户安装deploy。如果我登录到该服务器,它可以正常运行。

❯ ssh [email protected]
deploy@ubuntu-focal:~$ rbenv
rbenv 1.2.0-59-g0704e65
Usage: rbenv <command> [<args>...]

但是如果我们尝试通过 ssh 运行该命令,则找不到它。

❯ ssh [email protected] "rbenv"
bash: rbenv: command not found

脚本有同样的问题。

❯ ssh [email protected] 'bash -s' < configure.sh
ruby 3.2.1
bash: line 5: rbenv: command not found
bash: line 6: rbenv: command not found
bash: line 7: ruby: command not found
bash: line 9: gem: command not found
bash: line 10: gem: command not found

答案1

总之$PATH就是没设置。

Bash 在以交互模式或作为登录 shell 启动时,会读取一些配置文件,例如.profile.bashrc

--noprofile
  Do not read either the system-wide startup file /etc/profile  or
  any   of  the  personal  initialization  files  ~/.bash_profile,
  ~/.bash_login, or ~/.profile.   By  default,  bash  reads  these
  files  when  it  is invoked as a login shell (see INVOCATION be-
  low).

--norc Do  not  read  and  execute  the  personal  initialization  file
       ~/.bashrc if the shell is interactive.  This option is on by de-
       fault if the shell is invoked as sh.

man bash。您启动一个非登录、非交互式 shell。

或者,你的脚本应该:

  1. source ~/.bashrc
  2. $PATH自行指定
  3. 使用绝对路径

相关内容