Over a million developers have joined DZone.

Running A Leiningen/Ring Webapp As A Daemon Via Upstart (Ubuntu)

DZone's Guide to

Running A Leiningen/Ring Webapp As A Daemon Via Upstart (Ubuntu)

· Java Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

Running a Java/Clojure app as a daemon on Linux used to be hard but is pretty simple with Ubuntu Upstart (docs). The short story:

  1. Create an all-in one uberjar via “lein with-profile production ring uberjar” (using the lein-ring plugin; a simple lein uberjar would suffice for an app with a main- method)
  2. Create an upstart <service name>.conf file in /etc/init/
  3. Run sudo start/stop/status <service name>

And of course it works with Puppet too.

Example /etc/init/mongodiffer.conf

Example Upstart config file for the service mongodiffer:

## Upstart config file (use 'start mongodiffer', 'stop mongodiffer')
## Note: Stdout and stderr will be captured in /var/log/upstart/mongodiffer.log
## (aside of the native log in /var/log/mongodiffer.log)
author "Jakub Holy"
description "Start the MongoDiffer webapp on its default port (80)"
start on (local-filesystems and net-device-up IFACE!=lo)
# Note: "start on runlevel [2345]" would also do but I want to be explicit that
# running it w/o network is meaningless
# Must run as root to be able to run on port 80; ugly but quick
#setuid mongodiffer
#setgid mongodiffer
exec java -jar /srv/mongodiffer/clj-analytics-mongodiffer-standalone.jar
## TODO: Consider enabling respawning
# respawn
## Try to restart up to 10 times within 5 min:
# respawn limit 10 300

The only necessary “stanzas” are “start on” and “exec” (see also Upstart for Java apps). We could also enable respawning (both the stanzas) so that Upstart would try to start the service again if it crashes. (Of course Clojure service never crash ;-) )


I use tools.logging with logback (ch.qos.logback/logback-classic) and its RollingFileAppender with a SizeAndTimeBasedFNATP to keep the logs to a reasonable size:

<appender name="FILE">
<!-- rollover daily -->
<!-- or whenever the file size reaches the size -->
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<appender name="STDOUT">
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" /> <!-- Useful when running locally/dev -->

Stdout and stderr of the service is automatically captured in /var/log/upstart/mongodiffer.log via the default console log stanza. I haven’t been able to find out what to do to make sure that it won’t grow infinitely.

Serving static resources from a Ring uberjar

  1. Put the resources under a directory in resources, f.ex. resources/static/ (=> e.g. resources/static/js/zepto/zepto.min.js)
  2. Configure the wrap-resource Ring middleware: “(wrap-resource “static”)”
  3. Refer to the resources using the path after static (e.g. “/js/zepto/zepto.min.js”)

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.


Published at DZone with permission of Jakub Holý, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}