运行 远程主机上的脚本而不关心 ssh 会话

Run a script on remote host without caring ssh session

我在远程计算机的文件夹中有一个 python 脚本。为了执行它,我从我的本地计算机建立了一个 ssh 会话,转到该文件夹​​并 运行。

我使用的命令:

ssh remotehost
user@remotehost:~$ cd /my/folder
user@remotehost:~$ python abc.py >> abc.log

现在的问题是 ssh 会话。该脚本需要花费大量时间,并且由于互联网问题,ssh 会话终止且脚本未完成。假设遥控器始终处于开机状态并且 运行ning.

我可以 运行 脚本而不关心 ssh 会话终止并在我需要的任何时候使用 ssh tail -f abc.log 吗?

这取决于您要连接的远程计算机上安装了哪些工具。我知道的两个主要工具是 GNU screen 和 tmux。我使用屏幕,所以我会给你一些关于如何使用它的基础知识。您可以正常开始并在执行您的 Python 脚本之前进入屏幕会话:

ssh remotehost
user@remotehost:~$ cd /my/folder
user@remotehost:~$ screen
user@remotehost:~$ python abc.py >> abc.log

然后您可以脱离屏幕会话,即使您的 ssh 连接丢失,python 脚本也会保持 运行。

分离:

按 Ctrl-A 和 d

重新连接:

screen -r

要列出屏幕会话:

screen -ls

终止当前屏幕会话:

按 Ctrl-d

还有更多的功能需要筛选。这是入门的好资源:

更多详细信息请参阅手册:

您可以 运行 屏幕中的脚本或 运行 nohup+bg 中的进程。我总是喜欢屏幕,但让我解释一下这两种方法。

1. nohup

您可以使用 nohup 命令 运行 通过从终端分离进程 nohup python /my/folder/abc.py & 默认情况下,这将创建 nohup.out 文件,其中将存储所有日志。 如果你想要自定义文件,那么你可以使用重定向,那么它将是 nohup python /my/folder/abc.py >> abc.log &

在单个命令中它将是

ssh user@remotehost 'nohup python /my/folder/abc.py >> abc.log &'

nohup wikipedia

2. Screen

来自文档。

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal.

Screen Manual

因此您可以使用 ssh 直接 运行 屏幕中的脚本,然后您可以在需要时通过附加到屏幕来查看日志,或者您可以将日志直接重定向到某个文件或重定向到文件和使用 tee 输出。

运行 在屏幕中命令并在标准输出(终端)中打印输出。

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py; exec bash")'

运行 屏幕中的命令并将输出重定向到文件。

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py >> abc.log &2>1; exec bash")'

运行 屏幕中的命令并将输出重定向到文件和标准输出(终端)。

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py &2>1 |tee abc.log; exec bash")'

注意:在上述所有命令中,都需要执行 bash 否则屏幕将在作业完成后终止。

以上任何一个命令都可以完成这项工作。
在上述所有情况下,您都可以使用 screen -r ScreenName 附加屏幕 ScreenName 并可以查看日志。
重定向到文件时,我总是建议使用 stderr 重定向。

关于使用 linux 屏幕的一些参考资料

  1. 10 Screen Command Examples to Manage Linux Terminals
  2. How To Use Linux Screen