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

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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”)

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}