views:

110

answers:

5

Hello there!

I have build 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"?

Thanks in advice, guys! :)

A: 

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.

Faisal Feroz
This is good if you want to run a program periodically. The program should then terminate when its done.
KarlP
+1  A: 

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/

Grodriguez
+1  A: 

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 )

KarlP
+2  A: 

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 &

Chaitanya
JeremyP
+1  A: 

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 &. 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.

iirekm
On Debian, there is a pre-made script, that you can copy and adapt to your needs : `/etc/init.d/skeleton`
barjak