# <%= label %>
# Generated by strong-service-upstart on <%= created %>

description "<%= servicedescription %>"
author "<%= author %>"

# Start on boot/reboot
start on runlevel [2345]

# Stop on shutdown/reboot
stop on runlevel [016]

# To start/stop <%= label %> service manually, run:
#
# /sbin/initctl start <%= label %>
# /sbin/initctl stop <%= label %>

respawn

# User & Group to run service as:
<% if (cwd) { %>chdir <%= cwd %><% } else { %># No working directory specifed<% } %>

# Arbitrarily high limit on number of open file descriptors:
limit nofile 50000 50000

# Enable core dumps:
limit core unlimited unlimited

# Environment variables:
<% if (env && env.length > 0) {
  _.forEach(env, function(pair) {
  %>env <%- pair[0] %>=<%- pair[1] %>
<%}) } else {
  %># no environment variables set<%
} %>

# How to run the application
script

    # Abort if some part of this script fails
    set -e

    # Temporary fifo for sending log output to
    mkfifo /tmp/<%= label %>-log-fifo

    # A logger instance that pipes the fifo to syslog
    ( logger -t <%= label %> </tmp/<%= label %>-log-fifo & )

    # Redirect the current process's stdout to the logger
    exec >/tmp/<%= label %>-log-fifo

    # Clean up the fifo so we don't leave it laying around
    rm /tmp/<%= label %>-log-fifo

    # Run our actual application
    #  - stderr redirection is done last so that we don't lose error messages
    #    related to the Upstart config itself
    exec su -m -s /bin/sh -c 'exec "$0" "$@"' <%= user %> -- <%= execpath %> <%= script %> 2>&1

end script
