Running Jetty as a service in Ubuntu Linix

Jetty is a popular open source Java application server. In order to run it as a service on a Linux server, they recommend using a horribly overcomplicated and quite fragile script.

In Ubuntu server, there is a better way. Leverage Upstart and its declarative job definitions.

First install Jetty into /opt/jetty and create a jetty user:

useradd --user-group --shell /bin/false --home-dir /opt/jetty/temp jetty

Then create a file /etc/init/jetty.conf with content like this:

start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]

chdir /opt/jetty

setuid jetty

reload signal SIGQUIT

exec /usr/local/lib/jvm/bin/java \
-Xmx512m \
-Djava.awt.headless=true \
-Djetty.state=/opt/jetty/jetty.state \
-Djetty.port=8080 \
-Djetty.home=/opt/jetty \ \
-jar /opt/jetty/start.jar \
jetty.port=8080 \
etc/jetty-logging.xml \
etc/jetty-started.xml \
etc/jetty-requestlog.xml \
OPTIONS=plus etc/jetty-plus.xml \

This will replace both /etc/init.d/jetty and /etc/default/jetty.

If you run service jetty reload, it will create a thread dump (like jstack) in /var/log/upstart/jetty.log.

You can even add respwan to have the JVM automatically restarted if it unexpectedly quits for some reason.

A similar approach can be used for other JVM based servers, like Apache Tomcat.

This entry was posted in Java, Linux, Ubuntu. Bookmark the permalink.