SoFunction
Updated on 2024-11-12

Troubleshooting Python paramiko Module Remote Execution of ssh Command nohup Not Working

Python - Paramiko Module Remote Execution of the ssh Command nohup Doesn't Take Place

1、Use paramiko module ssh login to linux execute nohup command does not take effect

# Execute commands
def command(ssh_config, cmd, result_print=None, nohup=False):
  ssh = ()
  ssh.set_missing_host_key_policy(())
  (hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
        password=ssh_config.password)
  print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
  stdin, stdout, stderr = ssh.exec_command(cmd)
  result = ()
  if result_print:
    lines = read_unicode(result)
    for line in lines:
      print(line)
  ()

This is because the shell closes the channel as soon as the execution is finished

2. Use invoke_shell with a slight modification.

# Execute commands
def command(ssh_config, cmd, result_print=None, nohup=False):
  ssh = ()
  ssh.set_missing_host_key_policy(())
  (hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
        password=ssh_config.password)
  print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
  if nohup:
    cmd += ' & \n '
    invoke = ssh.invoke_shell()
    (cmd)
    # Wait for command execution to complete
    (2)
  else:
    stdin, stdout, stderr = ssh.exec_command(cmd)
    result = ()
    if result_print:
      lines = read_unicode(result)
      for line in lines:
        print(line)
  ()

To this point this article on the Python paramiko module to solve the problem of remote execution of the ssh command nohup does not take effect on this article, more related Python paramiko module remote execution of the ssh command nohup does not take effect on the contents of the module, please search for my previous articles or continue to browse the following related articles I hope that you will be more supportive of me in the future!