【问题标题】:sending script over ssh using ruby使用 ruby​​ 通过 ssh 发送脚本
【发布时间】:2013-01-16 05:52:32
【问题描述】:

我正在尝试用 ruby​​ 编写一个 bash 脚本,它将为我的一个应用程序启动一个 Resque 工作程序。

我从控制台中给出的参数生成的命令看起来像这样......

command = "ssh user@#{@ip} 'cd /path/to/app; bundle exec rake resque:work QUEUE=#{@queue}&'"
`command`

该命令已正确插入,一切看起来都很好。我被要求输入 ssh 命令的密码,然后什么也没有发生。我很确定我的语法对于建立 ssh 连接并在该连接中运行一行代码是正确的。 ssh user@host 'execute command'

我已经完成了一个更简单的命令,它只运行 mac say 终端命令并且运行良好

command = "ssh user@#{@ip} 'say #{@queue}'"
`command`

我在后台运行 rake 任务,因为我在 ssh 中使用过该行一次,如果你在后台运行该进程,它只会让工作人员保持活动状态。

有什么想法吗?谢谢!

【问题讨论】:

    标签: ruby bash ssh resque


    【解决方案1】:

    我想通了。

    这是一个 rvm 的事情。我需要在要运行的脚本的开头包含. .bash_profile

    所以... "ssh -f hostname '. .bash_profile && cd /path/to/app && bundle exec rake resque:work QUEUE=queue'" 是我让它工作所需要的。

    感谢@Casper 的帮助

    【讨论】:

      【解决方案2】:

      在命令参数启动的所有进程都完成之前,Ssh 不会退出会话。使用& 在后台运行它们并不重要。

      要解决这个问题,只需使用-f 开关:

      -f  Requests ssh to go to background just before command execution.  This is
          useful if ssh is going to ask for passwords or passphrases, but the user
          wants it in the background.  This implies -n.  The recommended way to start
          X11 programs at a remote site is with something like ssh -f host xterm.
      

      "ssh -f user@#{@ip} '... bundle exec rake resque:work QUEUE=#{@queue}'"
      

      编辑

      事实上,更仔细地研究这个问题,ssh 似乎只是在等待远程端关闭stdinstdout。您可以像这样轻松测试它:

      这挂了:

      ssh localhost 'sleep 10 &'
      

      这不会挂起:

      ssh localhost 'sleep 10 </dev/null >/dev/null &'
      

      所以我假设最后一个版本实际上非常接近使用 -f 运行。

      【讨论】:

      • 这似乎是正确的标志,但我仍然得到相同的结果。它提示输入密码登录,然后我得到我的本地终端提示。
      • @Luke - 如果您使用ssh -f xxx "... bundle exec ... &gt;/dev/null &lt;/dev/null &amp;" 会怎样? IE。将stdout/in 重定向添加到/dev/null
      • @Luke - 还要先用sleep 30 之类的东西对其进行测试,以确保问题不在其他地方。
      • dev/null 选项不起作用。我使用了 sleep 命令,它使它保持打开状态,但从未运行过其他命令。至少这是我假设的。可能是它们正在运行,但是抛出了一个错误,我看不到它。但是,我手动 ssh-ing 并在没有错误的情况下运行同一行,并且正在创建一个工作人员。我真的很困惑哈哈。
      猜你喜欢
      • 1970-01-01
      • 2018-02-15
      • 2011-09-12
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2014-01-20
      • 2015-08-02
      • 2014-11-02
      相关资源
      最近更新 更多