Remote command does not return python
Question: I am rebooting a remote machine through Python, as it is a reboot, the current
ssh session is killed. The request is not returned. I am not interested in the
return though.

os.command('sshpass -p password ssh user@host reboot')

The code is performing the reboot, however the current session is killed and
the script never returns.

I can do an async and just ignore the thread, any other easy options?


Answer: I'm surprised that the script doesn't return. The connection should be reset
by the remote before it reboots. You can run the process asyc, the one problem
is that subprocesses not cleaned up up by their parents become zombies (still
take up space in the process table). You can add a Timer to give the script
time to do its dirty work and then clean it up in the background.

Notice that I switched that command to a list of parameters and skipped
setting `shell=True`. It just means that no intermediate shell process is
executed.

    
    
    import sys
    import subprocess as subp
    import threading
    import timing
    
    def kill_process(proc):
        # kill process if alive
        if proc.poll() is None:
            proc.kill()
            time.sleep(.1)
            if proc.poll() is None:
                sys.stderr.write("Proc won't die\n")
                return
        # clean up dead process
        proc.wait()
    
    proc = subp.Popen(['sshpass', '-p', password, 'ssh', 'user@host', 'reboot')
    threading.Timer(5, kill_process, args=(proc,))
    
    # continue on your busy day...
    

