通过 ssh 运行持久进程

发布于 2025-01-05 02:13:38 字数 99 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小梨窩很甜 2025-01-12 02:13:38

作为 nohup 的替代方案,您可以在终端多路复用器内运行远程应用程序,例如 GNU screentmux

使用这些工具可以轻松地从另一台主机重新连接到会话,这意味着您可以在下班前结束长时间的构建或下载,并在回家后检查其状态。例如。我发现这在非常远程(在不同国家/地区)的服务器上进行开发工作时特别有用,并且我和它们之间的连接不可靠,如果连接断开,我可以简单地重新连接并继续,而不会丢失任何状态。

As an alternative to nohup, you could run your remote application inside a terminal multiplexor, such as GNU screen or 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.

晚雾 2025-01-12 02:13:38

如果您通过 SSH 连接到具有 systemd 的 Linux 发行版,则可以使用 systemd-run 在后台启动进程(用 systemd 的术语来说,就是“临时服务”)。例如,假设您想在后台 ping 某些内容:

systemd-run --unit=pinger ping 10.8.178.3

与仅使用 nohup 运行进程相比,使用 systemd 获得的好处是 systemd 将跟踪进程及其子进程,保留日志,记住退出代码并允许您干净地终止进程及其所有子进程。示例:

查看状态和输出的最后几行:

systemctl status pinger

流式输出:

journalctl -xfu pinger

Kill:

systemctl kill pinger

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:

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:

See the status and the last lines of output:

systemctl status pinger

Stream the output:

journalctl -xfu pinger

Kill:

systemctl kill pinger
笑着哭最痛 2025-01-12 02:13:38

是的;你可以使用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).

那片花海 2025-01-12 02:13:38

除了其他回复之外,您还可以通过 batch (或 < code>at),但正如 Brian 回答,你应该调用 daemon

你可以通过-f 选项 ssh

In addition to the other replies, you could start your test server thru batch (or at) but as Brian answered you should call daemon

And you could pass the -f option to ssh

古镇旧梦 2025-01-12 02:13:38

作为 nohupscreen 等的替代方案。您可以修改您的服务器以调用守护进程以将其与终端分离。这是为 Linux 编写服务的惯用方法。

另请参阅 daemon(3)

As an alternative to nohup, screen, et al. you could revise your server to invoke daemon to detach it from the terminal. This is the idiomatic way to write services for linux.

See also daemon(3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文