繁体   English   中英

使用python subprocess.Popen的Zombie SSH进程

[英]Zombie ssh process using python subprocess.Popen

我有一个脚本,该脚本使用2台不同的机器运行测试用例。 测试需要在机器1上运行某些命令,然后在机器2上运行命令,然后机器1将数据发送到机器2。目标是能够从任一机器上运行测试,因此我认为我将SSH到机器1和在此处执行命令,然后SSH到计算机2并在此处执行命令...我试图避免使用paramiko,因为我不希望额外的依赖关系,所以我发现了这段漂亮的代码可以完成这项工作:

def executeRemoteCommand(host, command):
  ssh = subprocess.Popen(["ssh", "%s" % host, command],
                         shell=False,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

这是我在做什么的一个例子:

executeRemoteCommand(user@machine1, "cd /dcd-netapp1/test/test-priority_v6; ./prerun")
time.sleep(30)  
executeRemoteCommand(user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./run")
time.sleep(1800)
executeRemoteCommand(user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./postrun")
executeRemoteCommand(user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./graph_results")

问题是我的prerun ssh会话没有被杀死。 postrun脚本负责杀死以prerun脚本启动的日志记录脚本,但是就像我说的那样,当我用ps -ef | grep ssh ps -ef | grep ssh

有关其他信息,我曾经在executeRemoteCommand函数中包含以下代码:

result = ssh.stdout.readlines()
if result == []:
  error = ssh.stderr.readlines()
  print >>sys.stderr, "ERROR: %s" % error
else:
  print result

我将其注释掉,因为prerun脚本将挂起,等待将std输出放入结果。 这永远不会发生。 我的prerun脚本确实具有std输出,但是我发现它无法收集,因为它可能是一种守护进程? 我对prerun脚本不太了解。

删除管道对我的情况有效。 现在,我从开始测试的计算机上的远程计算机获取所有输出,一切正常结束。 顺便说一句,我发现shell=False默认情况下为false,因此这是不必要的, %s % host字符串替换也是如此。 这是关于我的确切问题的样子:

def executeRemoteCommand(host, command):
  ssh = subprocess.Popen(["ssh", host, command])

由于现在对函数进行了超级简化,因此我进一步走了一步,发现如果完全Popen该函数并直接使用Popen则测试更容易阅读:

subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./prerun"])
time.sleep(5)
subprocess.Popen(["ssh", user@machine2, "cd /dcd-netapp1/test/test-priority-v6; ./run"])
time.sleep(1800)
subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./postrun"])
subprocess.Popen(["ssh", user@machine1, "cd /dcd-netapp1/test/test-priority-v6; ./graph_results"])

暂无
暂无

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

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