使用Python Paramiko在后台运行远程SSH服务器的过程 [英] Running process of remote SSH server in the background using Python Paramiko

查看:66
本文介绍了使用Python Paramiko在后台运行远程SSH服务器的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难使用Paramiko在后台在远程SSH服务器上运行进程.我用过:

I am finding hard to run a process on a remote SSH server at background using Paramiko. I used :

stdin, stdout, stderr = ssh.exec_command('executefile.py &') 

发现没有找到正在运行的 executefile.py 进程.

and found that no process of executefile.py was found running.

然后我尝试使用其他方式,包括反斜杠:

Then I tried using other way as including a backward slash:

stdin, stdout, stderr = ssh.exec_command('executefile.py \&') 

此方法有效.有一个实例在计算机上运行,​​但并不奇怪,它没有在后台运行.我可能知道它没有在后台运行,就像代码在此代码之后的第二行卡住了一样.是

This method worked. There was an instance running on machine but no surprise, it was not running at background. I could come to know as it is not running at background as when code stuck at second line after this code. It was

all_inf = stdout.readlines()

现在,除非脚本的进程被杀死,否则代码不会超出上一行.

Now code was not going beyond above line unless the process of the script was killed.

我正在学习Paramiko,感谢您的帮助.

I am learning Paramiko, any help is appreciated.

推荐答案

我已经尝试了此处和

I've tried all the methods described here and here without success, and finally realized that you need to use channels instead of using the SSHClient directly for calling exec_command (this does not work in background):

client = paramiko.SSHClient()
client.connect(ip_address, username='root', pkey=paramiko_key, timeout=5)
client.exec_command('python script.py > /dev/null 2>&1 &')

您应该创建和使用频道,此频道在后台运行:

You should create and use a channel, this works in background:

client = paramiko.SSHClient()
client.connect(ip_address, username='root', pkey=paramiko_key, timeout=5)
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command('python script.py > /dev/null 2>&1 &')

因此实际上并不需要 nohup dtach screen 等.

So nohup, dtach, screen, etc, are actually not necessary.

这篇关于使用Python Paramiko在后台运行远程SSH服务器的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆