简体   繁体   English

无法通过gem“ ssh-net”运行Ruby脚本

[英]Cant run Ruby script via gem “ssh-net”

I am using gem net-ssh . 我正在使用gem net-ssh

The connection is established. 建立连接。 I am able to run some simple scripts(like "pwd", "ls", etc.) successfully, but when I run the "restart" script I get an error. 我能够成功运行一些简单的脚本(例如“ pwd”,“ ls”等),但是当我运行“重新启动”脚本时,出现错误。

Here is a snippet: 这是一个片段:

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

And the output: 并输出:

./bin/restart:7:in exec': No such file or directory - bundle (Errno::ENOENT) from ./bin/restart:7:in ' Environment determined as "development" based on the .environment file. ./bin/restart:7:在exec': No such file or directory - bundle (Errno::ENOENT) from ./bin/restart:7:in在基于.environment文件确定为“开发”的环境中。 Loading application.yml 加载application.yml

And here is the script where the error occurs. 这是发生错误的脚本。

#!/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"

Connecting via Putty , when I run the same command it gets executed without any problems. 通过Putty进行连接,当我运行相同的命令时,它执行起来没有任何问题。

EDIT 编辑

The PATH variable is different when I 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 /home/csrhub/.rbenv/shims:/home/csrhub/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:在/ usr /游戏:在/ usr /本地/游戏:/卡/箱

And when I run with net-ssh PATH is: 当我使用net-ssh运行时,PATH是:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 在/ usr / local / sbin中:在/ usr / local / bin目录:/ usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中:在/ usr /游戏:在/ usr /本地/游戏

The problem is in your ssh.exec! 问题出在您的ssh.exec! call. 呼叫。 Net::SSH does not use a login shell to run your commands. Net::SSH不使用登录外壳程序来运行命令。 A login shell is required for Bash to execute .bash_profile . Bash需要登录shell 才能执行.bash_profile

Since your rbenv setup is in .bash_profile , your exec! 由于您的rbenv设置位于.bash_profile ,因此您的exec! call will be missing your full Ruby environment, and therefore running bundle later in you restart script will fail. 调用将缺少完整的Ruby环境,因此稍后在restart脚本中运行bundle将失败。

The solution should be to manually execute .bash_profile , so that rbenv gets loaded: 解决方案应该是手动执行.bash_profile ,以便加载rbenv:

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

More information and other proposals here: 更多信息和其他建议在这里:
net-ssh and remote environment 网络SSH和远程环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM