I want to start a java process using Upstart. Currently, on our OpenSuSE servers, I use the System V init system to achieve this, but on our Ubuntu servers I'd rather use Upstart. But I have two questions...

I have an Upstart job (a task) that configures the server, called, say, myconfig. And in the job that starts my java processes I ostensibly have:

start on stopped myconfig
exec /path/to/myjavastartscript.sh

myjavastartscript.sh runs 'java -classpath blah MyClass'. In System V init, starting the service runs 'nohup /path/to/myjavastartscript.sh &'.

So my first question is whether I still need to do the nohup or run-in-background with the exec command?

When running, MyClass starts other Java processes. In System V init, the service stop just looks for java processes owned by a certain user and kills them. My second question is how could I control the termination of these processes with Upstart?

2 Answers 2


You do not need to use nohup since when Upstart runs a program, that process will not be associated with a terminal (by default).

For Upstart, I'd suggest either just having the job call:

exec java -classpath blah MyClass

... or ensuring that /path/to/myjavastartscript.sh calls:

exec java -classpath blah MyClass

Note that the first exec above is an Upstart stanza whereas the 2nd is a shell keyword. If your shell script does not call the shell version of exec, you'll need to be careful to ensure you set the Upstart expect stanza correctly - see http://upstart.ubuntu.com/cookbook/#expect.

Regarding stopping the service, Upstart will automatically kill the process it is tracking (the main JVM process associated with MyClass) and any children of that process (technically any process in the same process group (see http://upstart.ubuntu.com/cookbook/#stopping-a-job)).

Without more details, I'm not sure your start on condition is suitable - presumably, you want the MyClass job start if and only if a configuration file has been setup? If so, the standard idiom is for the jobs pre-start stanza to read in /etc/default/MyClass.conf. If it decides that either the file doesn't exist, or the config file somehow indicates that the service is disabled / not setup correctly, the pre-start can simply call stop to stop the job from (fully) starting (see http://upstart.ubuntu.com/cookbook/#pre-start). The advantage of this approach being that your start on condition can then be reliably set to whatever set of conditions should cause the job to start. When those conditions are met, Upstart will run the job; the pre-start will run, determine the configuration is not yet valid and simply exit. The day the admin does decide to configure the service, the job will fully start.

See http://upstart.ubuntu.com/cookbook/#determining-the-start-on-condition-ubuntu-specific and http://upstart.ubuntu.com/cookbook/#ubuntu-well-known-events-ubuntu-specific for determining the start on condition.

Note finally that you should really always specify a stop on condition too. See http://upstart.ubuntu.com/cookbook/#stop-on.

  • You statement exec java -classpath blah MyClass is repeated twice without a change. Commented Oct 17, 2012 at 8:53
  • 1
    @MuhammadGelbana I believe that is on purpose. See his explanation in the following paragraph. Commented Oct 22, 2012 at 11:19
  • exec inside the start scripts works like a charm :) I wish I had known about it sooner, so I wouldn't have had to write the following monstrosity for a past project: java ...; JAVAPID=$!; trap "kill $JAVAPID" exit INT TERM; wait Commented Nov 10, 2014 at 16:47

I had the same problem, wanting to start the serviio dlna server when booting my machine.

This is my /etc/init/serviio.conf:

description "Start serviio DLNA server"
author "aanno"

start on net-device-up IFACE=eth0
stop on runlevel [!2345]
env enabled=1

setuid tpasch
setgid tpasch

#respawn the process if it crashes

env SERVIIO_HOME=/home/tpasch/serviio-1.0.1
        [ "$enabled" = "1" ] || [ "$force_start" = "1" ] || exit 0
        # Setup Serviio specific properties
        JAVA_OPTS="-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Dderby.system.home=$SERVIIO_HOME/library -Dserviio.home=$SERVIIO_HOME -Xmx512M -Xms20M -XX:+UseParNewGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20"

        # construct classpath
        cd $SERVIIO_HOME

        for i in lib/*.jar; do

        exec /usr/bin/java $JAVA_OPTS -classpath $CLASSPATH org.serviio.MediaServer
end script
  • I'm getting this error when I add the for loop: /proc/self/fd/9: 9: /proc/self/fd/9: Syntax error: word unexpected (expecting "do"). I'm having the loop the same as you mentioned it here. Thank you. Commented Oct 17, 2012 at 8:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .