2

I am reading some output of remote server using SSH bash(command). I need to do it constantly, so I spawn subprocess. It works fine, unless server server become unavailable for brief period. How do I restart SSH command (whole subprocess) if it fails?

I have following piece of code:

(...)

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
(...)

I would think that process.poll() is not None should do the trick but it seems to hang on

1000     21431  0.0  0.0      0     0 pts/0    Z+   Oct22   0:00 [ssh] <defunct>

And does not break out of while True:

Wojtas.Zet
  • 636
  • 2
  • 10
  • 30

1 Answers1

4

I had silly typo error in my code preventing if lines == "" and process.poll() is not None: from executing properly. Another thing to look at is ssh_config, It is wise to set values to disconnect @60 seconds/1 attempt. And avoid process.communicate() as it is blocking the whole thread.

Wojtas.Zet
  • 636
  • 2
  • 10
  • 30