1

我正在尝试用 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'

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

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

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

有什么想法吗?谢谢!

4

2 回答 2

1

我想到了。

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

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

感谢@Casper 的帮助

于 2013-01-15T21:52:32.730 回答
0

在命令参数启动的所有进程都完成之前,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.

IE

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

编辑

实际上,更仔细地查看问题似乎 ssh 只是在等待远程端关闭stdin并且stdout. 您可以像这样轻松测试它:

这挂起:

ssh localhost 'sleep 10 &'

这不会挂起:

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

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

于 2013-01-14T17:56:17.043 回答