【问题标题】:Paramiko starts a SSH proccess and it dies..why?Paramiko 启动了一个 SSH 进程,但它死了..为什么?
【发布时间】:2011-09-28 05:03:04
【问题描述】:

下面是我的代码。当登录到服务器并运行 ps aux | grep python 我看到所有的进程都开始了,然后在一两秒后死掉。如果我在服务器中运行命令..它可以工作。我试过 nohup.whithout nohup 等我没有解释。这是一个漫长的过程,需要数小时。

key = paramiko.RSAKey.from_private_key_file(rsa_private_key)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,port,username=username,pkey=key)
#stdin, stdout, stderr = ssh.exec_command('tar -xf /home/ubuntu/opt.tar.gz')
stdin, stdout, stderr = ssh.exec_command('ls')
#stdin, stdout, stderr = ssh.exec_command(bash)
stdin, stdout, stderr = ssh.exec_command('ls')
stdin, stdout, stderr = ssh.exec_command('export DISPLAY=localhost:0')
stdin, stdout, stderr = ssh.exec_command('nohup python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('nohup python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('nohup python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Optimization/pvServer2.py &')
stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Optimization/pvServer2.py &')
ssh.close()

【问题讨论】:

  • 如果你不输入&(如果你不退出paramiko),它是否还活着?
  • 不...试过...离开 & 没有帮助。我需要关闭 ssh 连接。
  • & 不会有任何影响,因为它由 shell 处理,例如 sh 或 bash,这里不会使用。

标签: python ssh paramiko


【解决方案1】:

尝试检查命令的输出。可能有一个错误被写入,你不会在你当前的代码中看到它。尝试做:

stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Optimization/pvServer2.py')
print 'exit_code: %d' % stdout.channel.recv_exit_status()
print stdout.read()
print stderr.read()

一旦您找出问题所在并修复它,您就可以返回使用 nohup。

我认为问题在于您调用 export DISPLAY 命令的方式。这不会影响您正在运行的其他命令的环境。你需要做这样的事情:

stdin, stdout, stderr = ssh.exec_command('sh -c "export DISPLAY=localhost:0; python /home/ubuntu/Optimization/pvServer2.py"')

【讨论】:

  • 感谢您的代码。当我运行代码时..该过程在服务器上执行,但 ssh 代码只是继续运行。我必须先退出该应用程序,但至少该进程继续在服务器上运行。 std err 和输出中没有打印任何内容。
  • 会不会是我正在执行一个扭曲的函数并启动一个反应器?该应用程序应该运行直到我杀死 ec2 实例
  • 我做了一个简单的测试。创建了一个包含两行的文件。而 True: x=2 并永远运行。是的,它运行。我怀疑这与扭曲有关。
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多