下面是我的代码。当登录到服务器并运行 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()
请您参考如下方法:
尝试检查命令的输出。可能写入了一个错误,并且您不会在当前代码中看到它。尝试做:
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"')
