开发者

Java running as a Unix service [duplicate]

This question already has answers here: 开发者_C百科 How to Daemonize a Java Program? (11 answers) Closed 7 years ago.

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?


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 &


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.


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 )


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/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜