2013-01-14 84 views
1

我正在嘗試在ruby中編寫一個bash腳本,該腳本將爲我的一個應用程序啓動一個Resque worker。使用ruby通過ssh發送腳本

,我從控制檯給出的PARAMS產生的命令看起來像這樣...

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

的命令是否正確插入,一切看起來不錯。我被要求輸入ssh命令的密碼,然後沒有任何反應。我很確定我的語法對於在該連接中進行ssh連接和運行一行代碼是正確的。 ssh [email protected] 'execute command'

我已經做了,只有運行在MAC say終端命令一個簡單的命令和工作的罰款

command = "ssh [email protected]#{@ip} 'say #{@queue}'" 
`command` 

我在後臺運行rake任務,因爲我已經使用該行一旦進入SSH如果你在後臺運行這個進程,它只會讓工作人員保持活着。

有什麼想法?謝謝!

回答

0

直到由command參數啓動的所有進程都已完成,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 [email protected]#{@ip} '... bundle exec rake resque:work QUEUE=#{@queue}'" 

編輯

其實這個問題尋找更加緊密似乎SSH只是在等待遠程端關閉stdinstdout。你可以這樣輕鬆地測試:

此掛起:

ssh localhost 'sleep 10 &' 

這不掛:

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

所以我假設的最後一個版本實際上是相當密切等同於-f運行。

+0

這似乎是正確的標誌使用,但我仍然得到相同的結果。它提示輸入密碼登錄,然後我得到我的本地終端提示。 – Luke

+0

@Luke - 如果你做了'ssh -f xxx「... bundle exec ...>/dev/null Casper

+0

@Luke - 首先用'sleep 30'來測試它,以確保問題不在其他地方。 – Casper

1

我想通了。

這是一件rvm的事情。我需要在我想運行的腳本的開始處包含. .bash_profile

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

感謝您的幫助@Casper