通过SSH执行远程python脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4180390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:38:49  来源:igfitidea点击:

Execute remote python script via SSH

pythonssh

提问by nka

I want to execute a Python script on several (15+) remote machine using SSH. After invoking the script/command I need to disconnect ssh session and keep the processes running in background for as long as they are required to.

我想使用 SSH 在多台(15 岁以上)远程机器上执行 Python 脚本。调用脚本/命令后,我需要断开 ssh 会话并保持进程在后台运行,只要它们需要。

I have used Paramiko and PySSH in past so have no problems using them again. Only thing I need to know is how to disconnect a ssh session in python (since normally local script would wait for each remote machine to complete processing before moving on).

我过去使用过 Paramiko 和 PySSH,所以再次使用它们没有问题。我唯一需要知道的是如何在 python 中断开 ssh 会话(因为通常本地脚本会在继续之前等待每个远程机器完成处理)。

采纳答案by David Dean

This might work, or something similar:

这可能有效,或类似的东西:

ssh [email protected] nohup python scriptname.py &

Basically, have a look at the nohupcommand.

基本上,看看nohup命令。

回答by chnrxn

On Linux machines, you can run the script with 'at'.

在 Linux 机器上,您可以使用“at”运行脚本。

echo "python scriptname.py" | at now

echo "python 脚本名.py" | 现在

回答by chnrxn

If you are going to perform repetitive tasks on many hosts, like for example deploying software and running setup scripts, you should consider using something like Fabric

如果您要在许多主机上执行重复性任务,例如部署软件和运行安装脚本,您应该考虑使用诸如Fabric 之类的东西

Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

Typical use involves creating a Python module containing one or more functions, then executing them via the fab command-line tool.

Fabric 是一个 Python(2.5 或更高版本)库和命令行工具,用于简化 SSH 在应用程序部署或系统管理任务中的使用。

它提供了一套基本的操作来执行本地或远程 shell 命令(通常或通过 sudo)和上传/下载文件,以及诸如提示运行用户输入或中止执行等辅助功能。

典型用途包括创建一个包含一个或多个函数的 Python 模块,然后通过 fab 命令行工具执行它们。

回答by Lahiru

You can even use tmuxin this scenario.

您甚至可以在这种情况下使用tmux

As per the tmuxdocumentation:

根据tmux文档:

tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more

tmux 是一个终端多路复用器。它使您可以在一个终端中的多个程序之间轻松切换、分离它们(它们继续在后台运行)并将它们重新附加到不同的终端。并做更多

From a tmux session, you can run a script, quit the terminal, log in again and check back as it keeps the session until the server restart.

在 tmux 会话中,您可以运行脚本、退出终端、再次登录并返回查看,因为它会保持会话直到服务器重新启动。

How to configure tmux on a cloud server

如何在云服务器上配置 tmux