Java 作为 Unix 服务运行

发布于 2024-09-26 22:24:25 字数 150 浏览 6 评论 0原文

我用 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 技术交流群。

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

发布评论

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

评论(5

入怼 2024-10-03 22:24:25

好吧,如果您想在退出 shell 时也运行 java 程序,以下是最简单的方法:

$nohup java -jar program.jar &

Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:

$nohup java -jar program.jar &
只是我以为 2024-10-03 22:24:25

您需要在 /etc/init.d 中创建适当的脚本并将其链接到 /etc/rcX.d 目录。该脚本应至少支持 startstopstatus 参数。在启动期间,它应该运行带有适当参数的 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 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.

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.

对你的占有欲 2024-10-03 22:24:25

你可以这样启动它:

java -jar program.jar

Unix 守护进程通常由 init 启动,或者由 /etc/init.d/etc/rc.d 中的脚本启动,并启动于特定的运行级别 - 通常通过 /etc/rcX.d 中的软链接。 (其中 X 是预期的“运行级别”,通常为 3。

我认为 debian 正在转向使用“upstart”,一个 init 替代品。它使用 /etc/init 中的配置文件来定义作业,它们很容易编写,

传统上,守护进程会关闭 stdin、sdtout 和 stderr,并在启动时执行“双分叉”,以便从会话中分离并发出信号,表明它们已准备好处理任何内容。这并不是真正必要的,只要守护进程不是从终端启动的,

如果您想要一个简单的 shell 包装器来启动您的程序,您只需要编写一个小 shell 脚本:

#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar

...并使其可执行。 (chmod 755)

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.

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:

#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar

... and make it executable (chmod 755 )

夜血缘 2024-10-03 22:24:25

本文包含一些将 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/

溇涏 2024-10-03 22:24:25

您可以使用 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.

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