【问题标题】:Cant run Ruby script via gem "ssh-net"无法通过 gem "ssh-net" 运行 Ruby 脚本
【发布时间】:2019-12-01 18:04:17
【问题描述】:

我正在使用 gem net-ssh。

连接已建立。我能够成功运行一些简单的脚本(如“pwd”、“ls”等),但是当我运行“restart”脚本时出现错误。

这是一个sn-p:

output = ssh.exec!("cd /home/csrhub/git/csrhub-api; ./bin/restart")
puts "#{output}"

还有输出:

./bin/restart:7:in exec': No such file or directory - bundle (Errno::ENOENT) from ./bin/restart:7:in' 基于 .environment 文件确定为“开发”的环境。 正在加载 application.yml

这是发生错误的脚本。

#!/usr/bin/env ruby

require 'yaml'
require_relative '../app/models/environment.rb'
config = Environment.config('application')

exec "bundle exec pumactl --control-url tcp://127.0.0.1:#{config['control_port']} --control-token #{config['control_token']} phased-restart"

通过Putty 连接,当我运行相同的命令时,它会毫无问题地执行。

编辑

我Putty时PATH变量不一样:

/home/csrhub/.rbenv/shims:/home/csrhub/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /bin:/usr/games:/usr/local/games:/snap/bin

当我使用 net-ssh 运行时,路径是:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

【问题讨论】:

  • 不要cd -- 只运行/home/csrhub/git/csrhub-api/bin/restart
  • @Zak 同样,还是不行。我得到同样的错误:(
  • 检查服务器上.bashrc 中的内容,它可能包含特定于 ruby​​ 的内容。将其复制到.bash_profile。交互式运行和非交互式运行是有区别的。
  • 似乎是PATH 问题。 ssh-net shell 会话的 PATH 与 Putty 会话不同。这可能是找不到bundle 命令的原因。

标签: ruby ssh


【解决方案1】:

问题出在您的ssh.exec! 电话中。 Net::SSH 不使用 login shell 来运行您的命令。登录 shell 是 required for Bash to execute .bash_profile。

由于您的 rbenv 设置位于 .bash_profile 中,因此您的 exec! 调用将丢失您的完整 Ruby 环境,因此稍后在您的 restart 脚本中运行 bundle 将失败。

解决办法应该是手动执行.bash_profile,让rbenv被加载:

output = ssh.exec!("source ~/.bash_profile; ...rest of commands...")

更多信息和其他建议在这里:
net-ssh and remote environment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    相关资源
    最近更新 更多