使用SSH在远程服务器上启动后台进程并退出会话 [英] Use SSH to start a background process on a remote server, and exit session

查看:117
本文介绍了使用SSH在远程服务器上启动后台进程并退出会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SSH在远程服务器上启动后台进程.这是我目前的情况:

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

ssh remote_user@server.com"nohup进程&"

ssh remote_user@server.com "nohup process &"

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

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

当我点击Ctr-C时,远程进程将继续在后台运行.

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

我想将ssh命令放在可以在本地运行的脚本中,所以我希望一旦远程进程启动,ssh会话就会自动退出.

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.

有没有办法做到这一点?

Is there a way to make this happen?

推荐答案

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

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

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

如果键入上面的内容,则将立即返回shell提示,然后您将看到"foo"输出.五秒钟后,您将看到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.

这篇关于使用SSH在远程服务器上启动后台进程并退出会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆