我正在使用 SSH bash(command
) 读取远程服务器的一些输出。我需要不断地这样做,所以我生成了子进程。它工作正常,除非服务器短时间内不可用。
如果失败,如何重新启动 SSH 命令(整个子进程)?
我有以下代码:
(...)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
while True:
line = process.stdout.readline()
if lines == "" and process.poll() is not None:
break
(...)
我认为 process.poll() is not None
应该可以解决问题
但似乎仍坚持
1000 21431 0.0 0.0 0 0 pts/0 Z+ Oct22 0:00 [ssh] <defunct>
并且不会突破while True:
最佳答案
我的代码中有愚蠢的拼写错误,导致 iflines == ""and process.poll() is not None:
无法正确执行。
另一件需要注意的事情是 ssh_config,明智的做法是设置值以断开 @60 秒/1 次尝试。
并避免 process.communicate(),因为它会阻塞整个线程。
关于python-3.x - python子进程失败时如何退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64534036/