How to run a Java Program as a daemon (service) on Linux (openSUSE) using a shell script

The following alternatives may be used to run a Java program as a daemon on Linux:

This article describes how to use a shell script. The disadvantage of the Java Wrapper and the Commons Daemon package is that they both make use of C programs. These C programs have to be compiled (or a matching binary distribution has to be found) and have to be compatible with the operating system and the installed Java runtime environment. A shell script, on the other hand, is easier to adapt to changing OS and Java environments.

Features

The shell script (javaDaemonTest.sh) provides the following functionality:

Installation Instructions

Example of how to install the JavaDaemonTest service (SUSE Linux, execute as root):
(Tested with SUSE/openSUSE 9.1, 10.0 and 11.1)

mkdir -p /var/local/javaDaemonTest
cd /var/local/javaDaemonTest
wget http://www.source-code.biz/snippets/java/javaDaemonTest.tar.gz
tar -xzf javaDaemonTest.tar.gz
javac JavaDaemonTest.java
./javaDaemonTest.sh install
rcjavaDaemonTest start
rcjavaDaemonTest status
... wait a bit to let the test program write some output lines into the log file ...
rcjavaDaemonTest stop
rcjavaDaemonTest status
less /var/log/javaDaemonTest.log

To uninstall:

rcjavaDaemonTest stop
rcjavaDaemonTest uninstall
rm -R /var/local/javaDaemonTest
rm /var/log/javaDaemonTest.log

Download

javaDaemonTest.sh    start/stop shell script (for SUSE Linux)
JavaDaemonTest.java   a dummy Java daemon program, used for testing the script
javaDaemonTest.tar.gz   the whole package as a tar/gzip file

Author: Christian d'Heureuse (www.source-code.biz, www.inventec.ch/chdh)
Index