Blog

- December 15, 2005

After spending quite a bit of time today figuring out how to properly register a Java-based server as a Linux service on Debian Sarge, I thought I would share the results of my findings. My requirements were:

  1. Automatically start, stop and restart
  2. Cleanly integrate with other system services
  3. Run the Java processes under a non-root user

Here is the script template that can be added to /etc/init.d :

#! /bin/sh
#
# -----------------------------------
# Initscript for the ...
# -----------------------------------
#
# This controls the ...
#
# Author:	Jerome Louvel .
#
# Version:	@(#)daemon  1.0  16-Dec-2005  ...@...
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="My daemon"
NAME=daemon

USER="prod"
HOME="/home/prod"
CP="...classpath..."
MAIN="...main class..."
ARGS="...main arguments..."

DAEMON="/usr/bin/java -server -cp $CP -Djava.awt.headless=true $MAIN $ARGS"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x /usr/bin/java || exit 0

# ---------------------------------------
# Function that starts the daemon/service
# ---------------------------------------
d_start()
{
su -p -s /bin/sh - $USER "$DAEMON &> /dev/null & echo $!" > $PIDFILE
}

# --------------------------------------
# Function that stops the daemon/service
# --------------------------------------
d_stop()
{
start-stop-daemon --stop --quiet --pidfile $PIDFILE
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart|force-reload)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac

exit 0

Here are the related resources I found:

Update: the script was modified from the initial post to follow more closely the /etc/init.d/skeleton script provided with Debian.

  • J Wilks

    Just curious, why use start-stop-daemon only on shutdown and not on startup? Thanks.

  • If I remember correctly, it didn’t work because the “java” executable requires arguments that conflicted with “start-stop-daemon” options. Externalizing the “java” executable invocation into a separate script didn’t work either. If you know how to start it with “start-stop-daemon”, please let me know!

  • There is an alternative solution that is also portable across many platforms: Java Service Wrapper. See: http://wrapper.tanukisoftware.org

  • Bill Stewart

    As for the conflict with java parameters, e.g. -D, you can append any parameters to start-stop-daemon by using a — before them, for example:

    CLASS=”your.program”
    CLASSPATH=”/your/paths/here”
    DAEMON=”/usr/bin/java”
    ARGS=”-server -cp $CLASSPATH
    -Djava.library.path=/usr/lib $CLASS”

    start-stop-daemon –start –quiet –pidfile $PIDFILE –background –exec $DAEMON — $ARGS

    Anything after the last “–” will be passed without modification as parameters to the –exec program

  • Bill Stewart

    In the above post before the $ARGS is a double dash, not a single. The HTML display makes it look like a single dash.

  • Can you also share an example java app? I still don’t get it how would the java app properly shutdown. Something like the OnStop() of VB.NET