I am writing a python script to do things on a remote computer. Actually the python script runs on my computer and the remote computer is a robot running ubuntu.
The command that I am issuing starts the robot software and doesn't exit (except if you kill it with ^c). I would like the command to be issued, be able to display the next 20 seconds of output (or until it goes "quiet") and then have my script actually complete.
I've been trying a combination of subprocess.run (and variants) and ssh options as well as ending the command with an ampersand. None of it works.
Here's what I have so far (you can see some experimental alternatives commented out and in). Everything I've tried either blocks at the ssh (waiting for subprocess to finish which it won't) or just runs through and I don't even see the prompt for the ssh password.
def ssh(self, command_line):
ssh_cmd = ["ssh", "pi@" + self.cfg["BRU_MASTER_IP"], command_line]
print(ssh_cmd)
print("1")
#cmd1 = subprocess.Popen(" ".join(ssh_cmd), shell=True, stdout=subprocess.PIPE)
completed_process = subprocess.run(" ".join(ssh_cmd), shell=True, capture_output=True, text=True)
#completed_process = subprocess.Popen(" ".join(ssh_cmd), shell=True, stdout=subprocess.PIPE)
print("2")
#completed_process.communicate()
print("2a")
#cmd1_out = completed_process.stdout.read()
print(completed_process.stdout.read())
print("3")
ssh … 'shell code'whereshell codeis shell code that runs1>log 2>&1 nohup start_robot_software &, thensleep 20, thencat logand then exits (so the localsshexits). I cannot help you in Python, therefore just a comment. Note ifstart_robot_softwaregenerates output indefinitely thenlogwill grow and grow.