Skip to content

Commit

Permalink
fix stderr stdout output handling for remote connections
Browse files Browse the repository at this point in the history
  • Loading branch information
pgehring committed Oct 14, 2024
1 parent fd0634a commit 5daeb29
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions robmuxinator/robmuxinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,22 @@ def send_cmd(self, cmd, wait_for_exit_status=True, get_pty=False):
if not self._use_local_connection:
self.init_connection()
stdin, stdout, stderr = self.ssh_cli.exec_command(cmd, get_pty=get_pty)

logger.debug(f"{cmd}")
if wait_for_exit_status:
returncode = stdout.channel.recv_exit_status()

stdout = stdout.read().decode()
stderr = stderr.read().decode()
else:
logger.debug(" using local connection")
process = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
text=True)
stdout = process.stdout
stderr = process.stderr
stdout = process.stdout.read()
stderr = process.stderr.read()

if wait_for_exit_status:
stdout_str, stderr_str = process.communicate()
Expand Down Expand Up @@ -235,13 +239,11 @@ def stop_session(self, session_name):
pid_session = None

if not returncode:
pid_session = stdout.readlines()
pid_session = pid_session[0].rstrip("\n") if len(pid_session) > 0 else None
pid_session = stdout[0].rstrip("\n") if len(stdout) > 0 else None
if pid_session:
cmd = "pgrep -P {}".format(pid_session)
returncode, stdout, stderr = self.send_cmd(cmd)
pid = stdout.readlines()
pid = pid[0].rstrip("\n") if len(pid) > 0 else None
pid = stdout[0].rstrip("\n") if len(stdout) > 0 else None
if not pid:
logger.error(f" session {session_name}: could not get process pid for session pid")
else:
Expand Down Expand Up @@ -533,8 +535,8 @@ def start(self):
)
logger.debug("pre_condition: {}".format(self._pre_condition))
logger.debug("ret: {}".format(ret))
logger.debug("stdout: {}".format(stdout.getvalue()))
logger.debug("stderr: {}".format(stderr.getvalue()))
logger.debug("stdout: {}".format(stdout))
logger.debug("stderr: {}".format(stderr))
if not ret:
break
time.sleep(0.25)
Expand Down

0 comments on commit 5daeb29

Please sign in to comment.