通过 ssh 运行持久进程
我正在尝试通过 ssh 启动测试服务器,但一旦我与 ssh 断开连接,它总是会死掉。
有没有办法启动一个进程(运行服务器),这样它就不会在我的 ssh 会话结束时终止?
I'm trying to start a test server via ssh but it always dies once i disconnect from ssh.
Is there a way to start a process (run the server) so it doesn't die upon the end of my ssh session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
作为
nohup
的替代方案,您可以在终端多路复用器内运行远程应用程序,例如 GNUscreen
或
tmux
。使用这些工具可以轻松地从另一台主机重新连接到会话,这意味着您可以在下班前结束长时间的构建或下载,并在回家后检查其状态。例如。我发现这在非常远程(在不同国家/地区)的服务器上进行开发工作时特别有用,并且我和它们之间的连接不可靠,如果连接断开,我可以简单地重新连接并继续,而不会丢失任何状态。
As an alternative to
nohup
, you could run your remote application inside a terminal multiplexor, such as GNUscreen
ortmux
.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.
如果您通过 SSH 连接到具有 systemd 的 Linux 发行版,则可以使用 systemd-run 在后台启动进程(用 systemd 的术语来说,就是“临时服务”)。例如,假设您想在后台 ping 某些内容:
与仅使用
nohup
运行进程相比,使用 systemd 获得的好处是 systemd 将跟踪进程及其子进程,保留日志,记住退出代码并允许您干净地终止进程及其所有子进程。示例:查看状态和输出的最后几行:
流式输出:
Kill:
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: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:See the status and the last lines of output:
Stream the output:
Kill:
是的;你可以使用
nohup
命令吞掉HUP(“hangup” ) 当您挂断 SSH 会话时发送到您的程序的信号。或者,如果您自己编写服务器,则可以对其进行编码以注册处理程序对于 HUP 信号,并将其吞入程序内部(而不是使用执行相同操作的外部
nohup
程序)。Yes; you can use the
nohup
command to swallow the HUP ("hangup") signal that is sent to your program when you hang up your SSH session.Alternatively, if you're writing the server yourself, you can code it to register a handler for the HUP signal, and swallow it inside the program (rather than using an external
nohup
program that does the same).除了其他回复之外,您还可以通过 batch (或 < code>at),但正如 Brian 回答,你应该调用
daemon
你可以通过
-f
选项 sshIn addition to the other replies, you could start your test server thru batch (or
at
) but as Brian answered you should calldaemon
And you could pass the
-f
option to ssh作为
nohup
、screen
等的替代方案。您可以修改您的服务器以调用守护进程
以将其与终端分离。这是为 Linux 编写服务的惯用方法。另请参阅
daemon(3)
。As an alternative to
nohup
,screen
, et al. you could revise your server to invokedaemon
to detach it from the terminal. This is the idiomatic way to write services for linux.See also
daemon(3)
.