2011-11-29 44 views
3

我似乎無法使Fabric能夠很好地處理我使用nohup的背景。 。 。給出各種信息應該是可能的,包括herehere無法使用Fabric和nohup真正背景SSH隧道

def test(): 
h = 'xxxxx.compute-1.amazonaws.com'  
ports = [16646, 9090, 6666] 

with settings(host_string = h): 
    tun_s = "ssh -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem %[email protected]%s " % (env.user, h)  

    for port in ports: 
     p_forward = "-L %d:localhost:%d" % (port, port) 
     tun_s = "%s %s" % (tun_s, p_forward) 

    tun_s = "%s -N" % tun_s 
    # create the tunnel. . . 
    print "creating tunnel %s" % tun_s 
    run("nohup '%s' >& /dev/null < /dev/null &" % tun_s) 
    print "fin" 

縮略的輸出:

[email protected]:~/deploy$ fab test 
executing on tunnel ssh -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem [email protected] -L 16646:localhost:16646 -L 9090:localhost:9090 -L 6666:localhost:6666 -N 
[xxx.compute-1.amazonaws.com] run: nohup 'ssh -o StrictHostKeyChecking=no -i ~/.ssh/kp.pem [email protected] -L 16646:localhost:16646 -L 9090:localhost:9090 -L 6666:localhost:6666 -N' >& /dev/null < /dev/null & 
fin 

Done. 
Disconnecting from xxxx 

我知道有與隧道命令本身沒有問題,因爲如果我奪走了nohup的東西,它工作正常(但顯然面料掛起)。我很確定它沒有正確分離,並且當運行函數返回隧道進程時立即死亡。

但是爲什麼?

這也發生在我的代碼的另一部分的python命令。

+0

爲什麼_你使用nohup?如果你問我,這是多餘的。 – sehe

+0

嗯,你是否嘗試使用-T禁用ssh的tty分配,並且還禁用pty = False的任務的tty分配? – favoretti

+0

@sehe,爲什麼它是多餘的?我還應該如何分離這個過程,以便布料可以在隧道不死的情況下返回? – Edwardr

回答

0

因此,似乎經過多次爭論,這是不可能的,無論出於什麼原因與我的設置(默認Ubuntu安裝EC2實例)。根據各種消息來源,我不知道爲什麼和如何可能。

我通過使用Paramiko代替Fabric來解決了我的特殊問題,因爲需要在後臺運行的調用仍在運行。以下實現:

import paramiko 
ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
privkey = paramiko.RSAKey.from_private_key_file('xxx.pem') 
ssh.connect('xxx.compute-1.amazonaws.com', username='ubuntu', pkey=privkey) 
stdin, stdout, stderr = ssh.exec_command("nohup ssh -f -o StrictHostKeyChecking=no -i  ~/.ssh/xxx.pem [email protected] -L 16646:localhost:16646 -L -N >& /dev/null < /dev/null &") 
ssh.close()