Linux 通过 ssh 运行持久进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9237385/
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
Run a persistent process via ssh
提问by 9-bits
I'm trying to start a test server via ssh but it always dies once i disconnect from ssh.
我正在尝试通过 ssh 启动测试服务器,但是一旦我与 ssh 断开连接,它总是会死机。
Is there a way to start a process (run the server) so it doesn't die upon the end of my ssh session?
有没有办法启动一个进程(运行服务器),这样它就不会在我的 ssh 会话结束时死掉?
采纳答案by Johnsyweb
As an alternative to nohup
, you could run your remote application inside a terminal multiplexor, such as GNU screen
or tmux
.
作为 的替代方案nohup
,您可以在终端多路复用器(例如GNUscreen
或tmux
.
Using these tools makes it easy to reconnect to a session from another host, which means that you can kick a long build or download off before you leave work and check on its status when you get home. For instance. I find this particularly useful when doing development work on servers that are very remote (in a different country) with unreliable connectivity between me and them, if the connection drops, I can simply reconnect and carry on without losing any state.
使用这些工具可以轻松地从另一台主机重新连接到会话,这意味着您可以在下班前启动长时间的构建或下载,并在回家时检查其状态。例如。我发现这在我和他们之间连接不可靠的非常远程(在不同国家)的服务器上进行开发工作时特别有用,如果连接断开,我可以简单地重新连接并继续,而不会丢失任何状态。
回答by ruakh
Yes; you can use the nohup
commandto swallow the HUP ("hangup") signal that is sent to your program when you hang up your SSH session.
是的; 当您挂断 SSH 会话时,您可以使用该nohup
命令来吞下发送到您的程序的 HUP(“挂断”)信号。
Alternatively, if you're writing the server yourself, you can code it to register a handlerfor the HUP signal, and swallow it inside the program (rather than using an external nohup
program that does the same).
或者,如果您自己编写服务器,您可以编写代码以注册HUP 信号的处理程序,并将其吞入程序中(而不是使用nohup
执行相同操作的外部程序)。
回答by Brian Cain
回答by Basile Starynkevitch
In addition to the other replies, you could start your test server thru batch(or at
) but as Brian answeredyou should call daemon
除了其他回复之外,您还可以通过批处理(或at
)启动您的测试服务器,但是当Brian 回答时,您应该致电daemon
And you could pass the -f
option to ssh
你可以将-f
选项传递给ssh
回答by Nikita
If you're SSHing to a Linux distro that has systemd, you can use systemd-run
to launch a process in the background (in systemd's terms, "a transient service"). For example, assuming you want to ping something in the background:
如果您通过 SSH 连接到具有 systemd 的 Linux 发行版,您可以使用systemd-run
它在后台启动一个进程(用 systemd 的术语来说,“临时服务”)。例如,假设您想在后台 ping 某些内容:
systemd-run --unit=pinger ping 10.8.178.3
The benefit you'll get with systemd over just running a process with nohup
is that systemd will track the process and its children, keep logs, remember the exit code and allow you to cleanly kill the process and all its children. Examples:
与仅运行进程相比nohup
,systemd的好处是 systemd 将跟踪进程及其子进程、保留日志、记住退出代码并允许您干净地终止进程及其所有子进程。例子:
See the status and the last lines of output:
查看状态和输出的最后几行:
systemctl status pinger
Stream the output:
流输出:
journalctl -xfu pinger
Kill:
杀:
systemctl kill pinger