Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
You statement exec java -classpath blah MyClass is repeated twice without a change. –  Muhammad Gelbana Oct 17 '12 at 8:53
    
@MuhammadGelbana I believe that is on purpose. See his explanation in the following paragraph. –  Martijn Heemels Oct 22 '12 at 11:19
add comment

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
respawn

env SERVIIO_HOME=/home/tpasch/serviio-1.0.1
script
        [ "$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
        CLASSPATH="config"

        for i in lib/*.jar; do
                CLASSPATH="$CLASSPATH:$i"
        done

        exec /usr/bin/java $JAVA_OPTS -classpath $CLASSPATH org.serviio.MediaServer
end script
share|improve this answer
    
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. –  Muhammad Gelbana Oct 17 '12 at 8:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.