bash 当应用程序可以作为系统服务运行时,为什么要使用 nohup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42327173/
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
Why use nohup when the app can be run as system service?
提问by rocksteady
I put this question on stackoverflow, because I found lots of quesions on the topic here already.
我把这个问题放在 stackoverflow 上,因为我已经在这里找到了很多关于这个主题的问题。
Short introduction
简介
Simply put, nohup
can be used to run apps in the background and keeps them running after the user logs off or the terminal or ssh session is closed e.g.
简单地说,nohup
可用于在后台运行应用程序,并在用户注销或终端或 ssh 会话关闭后保持它们运行,例如
There are many example quesitons here on stackoverflow, like thisor that.
stackoverflow 上有很多示例问题,例如this或that。
My question is simple.
我的问题很简单。
Why opt for nohup
, when there are options like upstart
, systemd
, ... which manage the app as service in a much more convenient way (runlevels, ...)?
为什么选择, , ... 之nohup
类的选项以更方便的方式(运行级别,...)将应用程序作为服务进行管理?upstart
systemd
Reading the many questions on similar topics, the only option seems to be nohup
. Almost never the answer is something like: "... use an upstart script, so it is all handled for you..."
阅读有关类似主题的许多问题,唯一的选择似乎是nohup
. 几乎从来没有答案是这样的:“......使用一个新贵脚本,所以这一切都为你处理......”
I would mainly go with e.g. upstart
, except maybe for a quick and dirty test scenario.
我将主要使用 eg upstart
,除了快速和肮脏的测试场景。
Am I missing something important?
我错过了什么重要的东西吗?
回答by Piotr Praszmo
nohup
is quick, easy, doesn't require root access and doesn't make permanent changes in the system. That's why many people use it (or try to use it) instead of configuring services.
nohup
快速、简单、不需要 root 访问权限并且不会对系统进行永久性更改。这就是为什么许多人使用它(或尝试使用它)而不是配置服务的原因。
Running things in the background without any supervision is usually a bad idea, although there are many legitimate use cases which don't fit traditional service model. For example:
在没有任何监督的情况下在后台运行东西通常是一个坏主意,尽管有许多不适合传统服务模型的合法用例。例如:
- Background process might be needed only sometimes, after certain user actions.
- More than one instance might be needed. For example: one per user or one per session.
- Process might not need to to be running all the time. It just quits after doing its job.
- 后台进程可能仅在某些用户操作之后才需要。
- 可能需要多个实例。例如:每个用户一个或每个会话一个。
- 进程可能不需要一直运行。它只是在完成工作后退出。
Some real world examples (which use something like nohup
and would be hard to implement as system services):
一些现实世界的例子(使用类似的东西nohup
并且很难作为系统服务来实现):
git
will sometimes rungit gc
in background to optimize repository without blocking user workadb
will start its service in background and keep it running until user asks to terminate it- Some compilers have option to keep running in the background to reduce startup times of subsequent invocations
git
有时会git gc
在后台运行以优化存储库而不阻止用户工作adb
将在后台启动它的服务并保持它运行直到用户要求终止它- 一些编译器可以选择在后台继续运行以减少后续调用的启动时间
回答by Julian
Your understanding is correct, the way those questions were asked the natural answer is nohup
- upstart
would be the answer to a different question such as How to make sure an application keeps running on Linux
您的理解是正确的,提出这些问题的方式自然是nohup
-upstart
将是不同问题的答案,例如如何确保应用程序在 Linux 上继续运行