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:
Configure start-stop-daemon to run the process as the given user, detach the process and automatically create a pid-file (I added EXTRA_ARGS to all calls to start-stop-daemon):
REXML could not parse this XML/HTML: <myuser> --background --make-pidfile"
The skeleton assumes that the process will be an instance of the command executed (not true because of the wrapper script) and not stop the process (because it thinks none is running). This can be fixed by replacing --exec with --startas in the script.
The skeleton also assumes the process will be named $NAME, which is not true, this can be fixed by removing --name $NAME.