Python execute remote command and don't wait for return

3.4k views Asked by At

I'm working on testing a corosync cluster. I'm trying to fail the interface that has the floating-IP to ensure the resource migrates over to another node with python.

Now the dilemma is my command does execute on the remote machine, but my test code hangs forever waiting for a reply it will never get--thenode will get rebooted because of the injected failure.

ssh = SSHClient(self.get_ms_ip(ms),
                        self.get_ms_user(ms),
                        self.get_ms_password(ms))
ssh.connect()
self.logger.info("Failing FIP eth now on %s" % ms)
ssh.exec_command(cmd, timeout=1)
#Code never reached this comment.

In python, how can I send the command and just continue on without waiting for any return? I've tried wrapping my ssh.exec_command with subprocess.Popen as suggested here Run Process and Don't Wait but that didn't yield anything different.

3

There are 3 answers

0
user597608 On BEST ANSWER

Python doesn't handle threads nicely; can't manually exit a thread. I ended up having to make a worker method that would create the shh connection and run exec_command that would be run as a seperate multiprocessing.Process.

This way I was able to cleanup after a test properly before the next test ran (as part of python's unit test framework).

0
Colin vH On

You don't want a subprocess, you want a thread. Spawn a thread that runs the exec_command call and you'll be able to continue with your code.

2
HayatoY On

Did you try nohup?

ssh.exec_command('nohup %s &'%cmd, timeout=1)