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
?

- 41,125
- 10
- 61
- 80

- 1,169
- 5
- 19
- 32
-
http://commons.apache.org/daemon/jsvc.html – jmj Oct 13 '10 at 10:22
-
Check in serverfault.com/superuser.com. System may would know more about this ( nohup, inittabs , cron etc) – Jayan Oct 13 '10 at 17:08
5 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 &

- 5,628
- 5
- 36
- 53

- 2,396
- 4
- 29
- 45
-
10consider redirecting stderr and stdout as part of the command. e.g. `nohup java .... 1>/dev/null 2>&1 &` – JeremyP Oct 13 '10 at 16:12
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.

- 5,628
- 5
- 36
- 53

- 8,890
- 5
- 36
- 46
-
On Debian, there is a pre-made script, that you can copy and adapt to your needs : `/etc/init.d/skeleton` – barjak Oct 13 '10 at 11:25
-
On Linux computers there is a utility command, `chkconfig`, which creates the links in the `/etc/rcX` directories for you by examining special format comments in your script. – Raedwald Nov 26 '13 at 21:10
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 )

- 5,628
- 5
- 36
- 53

- 5,149
- 2
- 28
- 41
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):

- 21,501
- 10
- 63
- 107
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.

- 12,458
- 4
- 40
- 51
-
This is good if you want to run a program periodically. The program should then terminate when its done. – KarlP Oct 13 '10 at 11:38