Remote command does not return python
<p>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.</p>

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

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

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

<p>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.</p>

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

<pre><code>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...
</code></pre>

python python-2.7 ssh