Paramiko启动一个SSH进程然后它就死了..为什么?

2024-09-30 10:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码。当登录到服务器并运行ps aux | grep python时,我看到所有的进程都在启动,然后在一两秒钟后结束。如果我在服务器上运行这个命令,它就可以工作了。我试过了没有。没有我无法解释。这是一个漫长的过程,需要几个小时。在

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()

Tags: keypy服务器paramikohomeubuntustderrstdin
1条回答
网友
1楼 · 发布于 2024-09-30 10:31:39

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

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命令的方式。这不会影响您正在运行的其他命令的env。你需要这样做:

^{pr2}$

相关问题 更多 >

    热门问题