【发布时间】:2016-02-26 07:14:25
【问题描述】:
我有一个使用 2 台不同机器运行测试用例的脚本。测试需要在机器 2 上运行命令之前在机器 1 上运行一些命令,然后机器 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
对于一些额外的信息,我曾经在executeRemoteCommand 函数中有这段代码:
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
我将其注释掉是因为prerun 脚本会挂起,等待将标准输出放入结果中。这永远不会发生。我的prerun 脚本确实有标准输出,但我认为它无法收集它,因为它可能是一种守护进程?我不太了解prerun 脚本。
【问题讨论】:
-
这里有一些可能有用的示例代码:github.com/NVIDIA/DIGITS/blob/v3.0.0-rc.1/digits/…
-
您希望您的命令终止吗?如果是这样, readlines 的事情应该工作。我的猜测是 ssh 被阻止等待密码。您可以使用
ssh-copy-id启用无密码登录。有一个名为askpass的程序也可以提供帮助。 -
是的,我希望
prerun在我运行postrun脚本时终止。我有authorized_keys。正如我想我提到的,没有 readlines 一切正常,但 ssh 不会终止,通常只是prerun但现在它们都还活着,应该在很久以前就结束了。 -
命令在远程端运行完成?如果你不关心 ssh 连接上发生了什么,你可以使用
nohup在后台运行。摆脱管道也是一个好主意,比如subprocess.Popen("nohup", ["ssh", "%s" % host, command], shell=False, stdout=open(subprocess.DEVNULL, 'wb'), stderr=subprocess.STDOUT) -
本地机器上的ssh是僵尸吗?
ps x条目为“Z”?
标签: python ssh subprocess popen zombie-process