简体   繁体   English

如何要求 SSH 上的远程服务器运行后台作业?

[英]How to ask a remote server over SSH to run a background job?

I'm trying to start a long-running process on a remote server, over SSH:我正在尝试通过 SSH 在远程服务器上启动一个长时间运行的进程:

$ echo Hello | ssh user@host "cat > /tmp/foo.txt; sleep 100 &"

Here, sleep 100 is a simulation of my long-running process.在这里, sleep 100是我长时间运行的过程的模拟。 I want this command to exit instantly, but it waits for 100 seconds.我希望这个命令立即退出,但它会等待 100 秒。 Important to mention that I need the job to receive an input from me ( Hello in the example above).重要的是要提到我需要这份工作来接收我的输入(上面示例中的Hello )。

Server:服务器:

$ sshd -?
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020

Saying "I want this command to exit instantly" is incompatible with "long-running".说“我希望这个命令立即退出”与“长时间运行”不兼容。 Perhaps you mean that you want the long-running command to run in the background.也许您的意思是您希望长时间运行的命令在后台运行。

If output is not immediately needed locally (ie. it can be retrieved by another ssh in future), then nohup is simple:如果本地不立即需要 output (即,将来可以由另一个 ssh 检索),那么nohup很简单:

echo hello |
ssh user@host '
    cat >/tmp/foo.txt;
    nohup </dev/null >cmd.out 2>cmd.err cmd &
'

If output must be received locally as the command runs, you can background ssh itself using -f :如果 output 必须在命令运行时在本地接收,您可以使用-f将 ssh 本身作为后台:

echo hello |
ssh -f user@host '
    cat >/tmp/foo.txt;
    cmd
' >cmd.out 2>cmd.err

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM