Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java running as a Unix service [duplicate]

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?

like image 801
Vilius Avatar asked Oct 13 '10 10:10

Vilius


People also ask

How do I stop and start Java in Linux?

Use jps to list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script.


2 Answers

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 &
like image 159
Chaitanya Avatar answered Oct 21 '22 09:10

Chaitanya


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.

like image 33
iirekm Avatar answered Oct 21 '22 09:10

iirekm