'How to run an ssh script such that it keeps executing through disconnects?
I want to run a Python script which calls remote commands over ssh.
I want some of the commands to continue even if the script or the connection dies.
Not a duplicate of this, which is the opposite.
My current code, which occasionally disconnects, is
import paramiko
def run_copy_script(sh_script_path):
assert os.path.isfile(sh_script_path)
script_stdout_log_path = os.path.splitext(sh_script_path)[0]
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# transport = client.get_transport()
# transport.set_keepalive(30)#causes an error
try:
client.connect(hostname="my_host", username="my_user", password="my_pass")
except Exception as e:
print(f"[!] Cannot connect to the SSH Server:. ERROR: {e}")
exit()
command = f"echo running {sh_script_path} && {sh_script_path} > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)"
stdin, stdout, stderr = client.exec_command(command)
Note: I am not looking for commands through shell.
I am aware things like nohup are possible, but am looking to remain inside Python.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
