Skip to main content
 首页 » 编程设计

远程服务器上的 Python 进程需要通过 ssh 连接到其他服务器并继续运行

2025年04月02日13落叶无声

有没有可能解决在远程服务器上运行的Python进程ssh到其他服务器然后继续运行?

run in 192.168.100.1 
| 
ssh to there 192.168.100.2 
| 
run continuously on 192.168.100.2 and do other functions from py 

我尝试使用子进程调用,但是当脚本调用 ssh 命令并连接到另一个时,它会停止并在那里等待。

请您参考如下方法:

您需要使用RPC调用远程服务器上的python函数。

周围有很多库。我们以RPyC为例库例如:

>>> import rpyc 
>>> c = rpyc.classic.connect("localhost") 
>>> c.execute("print 'hi there'")   # this will print on the host 
>>> import sys 
>>> c.modules.sys.stdout = sys.stdout 
>>> c.execute("print 'hi here'")   # now this will be redirected here 
hi here 

请注意,您需要在远程主机上安装 RPyC 服务器,并在那里部署您的 Python 模块。

请阅读tutorials学习如何启动 RPyC 服务器并在远程机器上部署代码