Linux 使用 SSH 在远程服务器上启动后台进程,并退出会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19996089/
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-07 01:21:23  来源:igfitidea点击:

Use SSH to start a background process on a remote server, and exit session

linuxsshremote-servernohup

提问by futureshocked

I am using SSH to start a background process on a remote server. This is what I have at the moment:

我正在使用 SSH 在远程服务器上启动后台进程。这就是我目前所拥有的:

ssh [email protected] "nohup process &"

ssh [email protected] "nohup 进程 &"

This works, in that the process does start. But the SSH session itself does not end until I hit Ctr-C.

这是有效的,因为该过程确实开始了。但是 SSH 会话本身直到我按下 Ctr-C 才结束。

When I hit Ctr-C, the remote process continues to run in the background.

当我按下 Ctr-C 时,远程进程继续在后台运行。

I would like to place the ssh command in a script that I can run locally, so I would like the ssh session to exit automatically once the remote process has started.

我想将 ssh 命令放在可以在本地运行的脚本中,因此我希望 ssh 会话在远程进程启动后自动退出。

Is there a way to make this happen?

有没有办法做到这一点?

回答by Majlik

You could use screento run your process on this screen, detach from screen Ctrl-a :detachand exit your current session without problem. Then you can reconnect to SSH and attach to this screen again to continue with your task or check if is finished.

您可以使用screen在此屏幕上运行您的进程,从屏幕上分离Ctrl-a :detach并毫无问题地退出当前会话。然后您可以重新连接到 SSH 并再次附加到此屏幕以继续您的任务或检查是否已完成。

Or you can send the command to an already running screen. Your local script should look like this:

或者您可以将命令发送到已经运行的屏幕。您的本地脚本应如下所示:

ssh [email protected]
screen -dmS new_screen sh
screen -S new_screen -p 0 -X stuff $'nohup process \n'
exit    

For more info see this tutorial

有关更多信息,请参阅本教程

回答by Douglas

The "-f" option to ssh tells ssh to run the remote command in the background and to return immediately. E.g.,

ssh 的“-f”选项告诉 ssh 在后台运行远程命令并立即返回。例如,

ssh -f user@host "echo foo; sleep 5; echo bar"

If you type the above, you will get your shell prompt back immediately, you will then see "foo" output. Five seconds later you will then see "bar" output. In the meantime, you could have been using the shell.

如果您键入上述内容,您将立即返回您的 shell 提示符,然后您将看到“foo”输出。五秒钟后,您将看到“bar”输出。同时,您可能一直在使用 shell。

回答by Ronny Andersson

When using nohup, make sure you also redirect stdin, stdout and stderr:

使用时nohup,请确保您还重定向 stdin、stdout 和 stderr:

ssh user@server 'DISPLAY=:0 nohup xeyes < /dev/null > std.out 2> std.err &'

In this way you will be completely detached from the remote process. Be carefull with using ssh -f user@host...since that will only put the ssh process in the background on the callingside. You can verify this by running a ps -aux | grep sshon the calling machine and this will show you that the ssh call is still active, but just put in the background.

通过这种方式,您将完全脱离远程进程。使用时要小心,ssh -f user@host...因为这只会将 ssh 进程置于调用方的后台。您可以通过ps -aux | grep ssh在调用机器上运行 a 来验证这一点,这将显示 ssh 调用仍然处于活动状态,但只是放在后台。

In my example above I use DISPLAY=:0since xeyes is an X11 program and I want it started on the remotemachine.

在我上面的例子中,我使用DISPLAY=:0xeyes 是一个 X11 程序,我希望它在远程机器上启动。