Java 作为 Unix 服务运行
我用 Java 构建了一个小守护进程,我想将它作为 Unix 下的服务运行(例如 Debian 5)。我读到可以使用 Java 包装器,但是没有其他更容易实现的选项吗?我不能只使用 Unix 命令,例如 xxx java -jar program.jar
吗?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,如果您想在退出 shell 时也运行 java 程序,以下是最简单的方法:
Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:
您需要在
/etc/init.d
中创建适当的脚本并将其链接到/etc/rcX.d
目录。该脚本应至少支持start
、stop
和status
参数。在启动期间,它应该运行带有适当参数的java
命令,可能通过nohup java; &
。然后,您应该将新启动的进程的 PID 保存到文件/var/run/yourservice.pid
中。stop
命令应读取此 PID 文件并终止此服务。细节因发行版而异,大多数发行版都提供一些宏以使整个工作变得更容易。最好查看
/etc/init.d
中适合您的发行版的其他服务示例。此外:
如果您的服务无法从网络中的其他计算机访问,但它打开了某些端口,请通过防火墙使其不可用。
如果您的服务处理一些“敏感”数据,最好添加另一个用户并在
/etc/init.d
文件中调用适当的sudo
命令。You need to create an appropriate script in
/etc/init.d
and link it to/etc/rcX.d
directories. The script should support at leaststart
,stop
, andstatus
parameters. During start it should runjava
command with appropriate arguments, probably vianohup 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.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.你可以这样启动它:
Unix 守护进程通常由 init 启动,或者由
/etc/init.d
或/etc/rc.d
中的脚本启动,并启动于特定的运行级别 - 通常通过/etc/rcX.d
中的软链接。 (其中 X 是预期的“运行级别”,通常为 3。我认为 debian 正在转向使用“upstart”,一个 init 替代品。它使用
/etc/init
中的配置文件来定义作业,它们很容易编写,传统上,守护进程会关闭 stdin、sdtout 和 stderr,并在启动时执行“双分叉”,以便从会话中分离并发出信号,表明它们已准备好处理任何内容。这并不是真正必要的,只要守护进程不是从终端启动的,
如果您想要一个简单的 shell 包装器来启动您的程序,您只需要编写一个小 shell 脚本:
...并使其可执行。 (chmod 755)
You can start it as:
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.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.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.
If you want a simple shell wrapper to start you program; you just need to write a small shell script:
... and make it executable (chmod 755 )
本文包含一些将 Java 应用程序作为守护进程运行的有用技巧:
http:// bareenough.org/blog/2005/03/java-daemon/
或者,您可以查看 Apache Commons Daemon 项目,尽管这需要本机代码(支持 Unix 和 Win32):
http://commons.apache.org/daemon/
This article contains a few useful tricks for running a Java application as a 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):
http://commons.apache.org/daemon/
您可以使用 cron 作业来安排您的程序。您还可以查看这篇文章,了解有关如何在启动时运行脚本的详细信息。您可以编写一个运行 java 程序的脚本,并在启动时运行它,如文章中所述。
You can use a cron job to schedule your program. You can also check out this article 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.