Java 作为 Unix 服务运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3922554/
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
Java running as a Unix service
提问by Vilius
I have built a little daemon in Java and I would like to run it as a service under Unix (e.g. Debian 5). I have read that there is a possibility of using a Java wrapper, but isn't there any other option which is easier to implement? Can't I just use a Unix command such as xxx java -jar program.jar
?
我已经用 Java 构建了一个小守护进程,我想在 Unix(例如 Debian 5)下将它作为服务运行。我已经读到有可能使用 Java 包装器,但是没有其他更容易实现的选项吗?我不能只使用 Unix 命令xxx java -jar program.jar
吗?
采纳答案by Chaitanya
Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:
好吧,如果你想在退出 shell 时运行你的 java 程序,下面是最简单的方法:
$nohup java -jar program.jar &
回答by Faisal Feroz
You can use a cron job to schedule your program. You can also check out thisarticle for details on how to run scripts on startup. You can write a script that runs your java program and run it on startup as mentioned in the article.
您可以使用 cron 作业来安排您的程序。您还可以查看这篇文章,了解有关如何在启动时运行脚本的详细信息。您可以编写一个脚本来运行您的 Java 程序并按照文章中所述在启动时运行它。
回答by Grodriguez
This article contains a few useful tricks for running a Java application as a daemon:
本文包含一些将 Java 应用程序作为守护程序运行的有用技巧:
http://barelyenough.org/blog/2005/03/java-daemon/
http://barelyenough.org/blog/2005/03/java-daemon/
Alternatively, you can have a look at the Apache Commons Daemon project, although this requires native code (Unix and Win32 supported):
或者,您可以查看 Apache Commons Daemon 项目,尽管这需要本机代码(支持 Unix 和 Win32):
回答by KarlP
You can start it as:
您可以将其启动为:
java -jar program.jar
Unix daemons are normally started by init or started by a script in /etc/init.d
or /etc/rc.d
, and started at specific runlevels - normally by soft links in /etc/rcX.d
. (where X is the intended "runlevel" which is normally 3.
Unix的守护进程通常由init启动或通过脚本启动/etc/init.d
或者/etc/rc.d
,在特定的运行级别开始-通常是由软链接/etc/rcX.d
。(其中 X 是预期的“运行级别”,通常为 3。
I think debian are moving to using "upstart", a init-replacement. It uses config files in /etc/init
to define jobs, and they are quite easy to write. Check that out.
我认为 debian 正在转向使用“新贵”,一个 init 替代品。它使用配置文件/etc/init
来定义作业,而且它们很容易编写。检查出。
Daemons traditionally closes stdin, sdtout and stderr, and does a "double fork" when starting, in order to detach from the session and also to signal that they are ready to handle whatever they should handle. This is not really necessary, as long as the daemon is not started from the terminal.
守护进程传统上关闭 stdin、sdtout 和 stderr,并在启动时执行“双叉”,以便从会话中分离并表示它们已准备好处理应该处理的任何事情。只要守护程序不是从终端启动的,这并不是真正必要的。
If you want a simple shell wrapper to start you program; you just need to write a small shell script:
如果你想要一个简单的 shell 包装器来启动你的程序;你只需要写一个小的shell脚本:
#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar
... and make it executable (chmod 755 )
...并使其可执行(chmod 755)
回答by iirekm
You need to create an appropriate script in /etc/init.d
and link it to /etc/rcX.d
directories. The script should support at least start
, stop
, and status
parameters. During start it should run java
command with appropriate arguments, probably via nohup java <arguments> &
. Then you should save PID of your newly-started process to file /var/run/yourservice.pid
. stop
command should read this PID file and kill this service.
The details vary from distribution to distribution, most distributions provide some macros to make whole job easier. It's best to look at examples of other services in /etc/init.d
for your distribution.
您需要在其中创建适当的脚本/etc/init.d
并将其链接到/etc/rcX.d
目录。该脚本至少应支持start
,stop
和status
参数。在启动期间,它应该java
使用适当的参数运行命令,可能通过nohup java <arguments> &
. 然后,您应该将新启动的进程的 PID 保存到 file /var/run/yourservice.pid
。stop
命令应该读取这个 PID 文件并终止这个服务。细节因发行版而异,大多数发行版提供了一些宏以使整个工作更容易。最好查看/etc/init.d
您的发行版中其他服务的示例。
Additionally: If your service isn't accessed from other computers from the network, but it opens some port, make it unavailable with firewall.
另外:如果您的服务未从网络中的其他计算机访问,但它打开了某些端口,请使用防火墙使其不可用。
If your service processes some 'delicate' data, it's good to add another user and invoke an appropriate sudo
command in your /etc/init.d
file.
如果您的服务处理一些“敏感”数据,最好添加另一个用户并sudo
在您的/etc/init.d
文件中调用适当的命令。