Running Java applications as daemon on Debian Linux

Debian has start-/stop scripts in /etc/init.d/ which are documented in the Debian Policy Manual. An example is available in /etc/init.d/skeleton. These scripts use start−stop−daemon to start and stop a background process. So to configure a start/stop script for a Java application, copy the skeleton f.e. to /etc/init.d/myserver and customize the settings:

...

# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network

...

DESC="My Server"
NAME=myserver
DAEMON=/opt/myserver/bin/$NAME
...

Then update-rc.d is executed to update the scripts for the run levels:

update-rc.d myserver defaults

If the application needs additional arguments (like log4j.configuration) I recommend to created a little wrapper script like bin/myserver so you don’t need to keep this configuration in the init.d script:

#!/bin/sh

cd $(dirname $0)/../
exec java -Dlog4j.configuration=file:./conf/log4j.properties -jar bin/felix.jar

The exec here is important, it makes the started java process replace the script process, so that a) the process id doesn’t change and b) we don’t get an additional process because of the script.

The default skeleton start/stop script expects the Java application to behave like a proper daemon: it should detach itself from the parent process. There is a way to make a Java application do this: jsvc. For this to work, the software needs to implement a Daemon interface from Commons Daemon. The big advantage of this approach is that the Java process can perform actions as root (for example open privileged ports) before switching to a non-root-user.

If you don’t want to go that way, there is a “last resort way” to work around this issue using options for start-stop-daemon:

EXTRA_ARGS="-c <myuser> --background --make-pidfile"

About the author

Ralf Ebert Ralf Ebert is an independent software developer and trainer for Mac OS X and iOS. He makes the Page Layers, TakeOff and Straight ahead apps and conducts iOS trainings and Git trainings in Germany.