jump to navigation

Java services on Linux December 15, 2005

Posted by Jerome Louvel in Java, Linux.
trackback

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.

Advertisements

Comments»

1. J Wilks - February 14, 2006

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

2. noelios - February 14, 2006

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!

3. noelios - March 24, 2006

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

4. Bill Stewart - January 24, 2007

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

5. Bill Stewart - January 24, 2007

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

6. moodsey211 - November 18, 2011

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s