在 Linux 上守护 Java 应用程序的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2724725/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:32:06  来源:igfitidea点击:

Best way to daemonize Java application on Linux

javalinuxdaemon

提问by SyBer

While I found this question being answered here on SW several times, I didn't find a concluding answer what is the best approach.

虽然我发现这个问题在 SW 上得到了多次回答,但我没有找到一个结论性的答案,什么是最好的方法。

I'm not looking to use any external wrapper, as I found them launching the java process under a nice level lower then themselves which potentially lowers the performance, so it seems only the shell methods are left.

我不打算使用任何外部包装器,因为我发现它们在低于自身的良好级别下启动 java 进程,这可能会降低性能,因此似乎只剩下 shell 方法。

I so far found 3 different shell methods:

到目前为止,我发现了 3 种不同的 shell 方法:

  • start-stop-daemon
  • RedHat daemon init.d function
  • nohup on start / disown after start
  • 启动停止守护进程
  • RedHat 守护进程 init.d 函数
  • nohup on start / disown 启动后

What you people are using, and can recommend as the most reliable method?

你们正在使用什么,并且可以推荐为最可靠的方法?

Thanks.

谢谢。

采纳答案by itsadok

While the standard answer to this seems to be jsvc, I have been using djb's daemon toolsto be a great way to make anything into a daemon.

虽然对此的标准答案似乎是jsvc,但我一直在使用 djb 的守护程序工具作为将任何东西变成守护程序的好方法。

I have java, python and a few shell scripts all running as daemons, with a simple way to start/stop them and great logging.

我有 java、python 和一些 shell 脚本,它们都作为守护进程运行,有一个简单的方法来启动/停止它们和很好的日志记录。

I've used to run daemontools itself as root on initctl as originally designed, but after a few months I decided to run it manually, under a normal user, and using svscan-startfor nicer logging.

我曾经按照最初设计的那样在 initctl 上以 root 身份运行 daemontools,但几个月后我决定在普通用户下手动运行它,并使用svscan-start进行更好的日志记录。

回答by danben

If I want to run an application in the background as a daemon, I do it like this:

如果我想在后台运行一个应用程序作为守护进程,我这样做:

nohup java -jar MyJar &

nohup java -jar MyJar &

There's nothing particularly unreliable about it - nohupkeeps it from receiving a SIGHUPwhen you disconnect, and &runs a process in the background.

没有什么特别不可靠的地方 -当你断开连接时nohup让它不接收SIGHUP,并&在后台运行一个进程。

Optionally, you can redirect the output to something other than nohup.out.

或者,您可以将输出重定向到nohup.out.

回答by Vadzim

Take a look at http://yajsw.sourceforge.net/. It's free and somewhat compatible reimplementation of TanukiSoftware Java Service Wrapper featuring free 64-bit support.

看看http://yajsw.sourceforge.net/。它是 TanukiSoftware Java Service Wrapper 的免费且有些兼容的重新实现,具有免费的 64 位支持。

There is also a comparison table for YAJSW, JSW, ACDand L4J.

还有一个比较表YAJSWJSWACDL4J

回答by Chris Mo

I would use the RedHat daemon init.d function. This allows you to have the application restart when the server does. Running nohup doesn't handle server restarts, etc.

我会使用 RedHat 守护进程 init.d 函数。这允许您在服务器重启时重新启动应用程序。运行 nohup 不处理服务器重启等。

回答by gpampara

I've used Akumain the past, with great success. The fact that it's a Java library really makes things easier, embedding the functionality is trivial.

我过去曾使用过Akuma,并取得了巨大的成功。它是一个 Java 库这一事实确实使事情变得更容易,嵌入功能很简单。

回答by crowne

I wouldn't rule out the external wrappers completely.
I have noticed some well respected software using the Tanuki Wrapper http://wrapper.tanukisoftware.org/.

我不会完全排除外部包装器。
我注意到一些使用 Tanuki Wrapper http://wrapper.tanukisoftware.org/ 的备受推崇的软件。

This has the added advantage that the program can also be easily run as a windows service, with a consistent approach.

这具有额外的优势,即程序还可以通过一致的方法轻松地作为 Windows 服务运行。

回答by Mitch Dempsey

You could try using screen(Launch screen, then launch the java program, then close[not quit] the screen)

您可以尝试使用screen(启动屏幕,然后启动java程序,然后关闭[不退出]屏幕)