This article covers setting up an Upstart Job on an Ubuntu
12.04 server. This upstart job will start/stop a program packaged in a jar
file.
For lots of details on upstart you can go to http://upstart.ubuntu.com/cookbook/
[1]
Creating a jar file with a manifest
To make life easier I am going to install ant to use to
compile the code and create the jar file.
> sudo apt-get install ant
|
Create the code
> cd
> mkdir jar-test
> cd jar-test
> mkdir -p src/com/whiteboardcoder/hello
> mkdir target
> vi
src/com/whiteboardcoder/hello/hw.java
|
Put this in the java file
package com.whiteboardcoder.hello;
import
java.util.Date;
import
java.util.TimeZone;
import
java.util.Locale;
import
java.text.DateFormat;
import
java.text.SimpleDateFormat;
public class hw {
public static String getISO8601Now(){
DateFormat dateFormat = new
SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'",
Locale.US);
dateFormat.setTimeZone(
TimeZone.getTimeZone("UTC"));
return dateFormat.format(new Date());
}
public static void main(String...args){
TimeZone tz =
TimeZone.getTimeZone("UTC");
try{
while(true){
System.out.println(
"Hello World The UTC time
in ISO 8601 format is "
+ hw.getISO8601Now());
Thread.sleep(2000);
}
}
catch(Exception e){
}
}
}
|
Create build.xml file for the ant build
> vi build.xml
|
And place the following in it
<project
default="main" basedir=".">
<target name="main">
<echo>Compiling code and creating
Jar file</echo>
<javac srcdir="src"
destdir="target"
/>
<jar basedir="target"
destfile="HW.jar">
<manifest>
<attribute name="Main-Class"
value="com.whiteboardcoder.hello.hw" />
</manifest>
</jar>
</target>
</project>
|
Run the ant build and create the executable jar file
> ant
|
Now run the executable jar
> java -jar HW.jar
|
This will output the UTC time in ISO 8601 format every 2
seconds
I just wanted to create a simple program that would keep
putting output out so that we could see that it started/stopped in the upstart
job
Create the upstart job
I found a few sites giving suggestions on how to make an
upstart job to run a java jar file. http://stackoverflow.com/questions/12102270/run-java-jar-file-on-a-server-as-background-process
[2] has an upstart job that I used (adjusted it a little) http://askubuntu.com/questions/162768/starting-java-processes-with-upstart
[3] has a more complex upstart example.
Another site I used for some information on upstart stanzas
is at http://upstart.ubuntu.com/wiki/Stanzas
[4]
Now to the nitty gritty create the /etc/init/helloworld.conf
> sudo vi /etc/init/helloworld.conf
|
This is the contents of my upstart job
description
"Hello World"
author
"Patman"
start on runlevel
[2345]
stop on runlevel
[!2345]
#Respawn the
process if it crashes
#If it respawns
more than 10 times in 5 seconds stop
respawn
respawn limit 10 5
expect fork
script
cd /home/patman/jar-test
java -jar HW.jar >
/var/log/HW-test.log 2>&1
end script
|
Save it and start it
> sudo start helloworld
|
Then look at the output being saved at /var/log/HW-test.log
> sudo start helloworld
|
Now tail the output file
> sudo tail -f /var/log/HW-test.log
|
To stop the program run the following
> sudo stop helloworld
|
If you tail the log you will see that it has stopped J
References
[1] Upstart Intro, Cookbook and Best Practises
Accessed
01/2014
[2] Run java jar file on a server as background process
Accessed
01/2014
[3] Starting java processes with Upstart
Accessed
01/2014
[4] Upstart Stanza
Accessed
01/2014
Thanks! It was really helpful and straight forward.
ReplyDeleteSee this blog was posted a while ago - but it still helped me today! Thanks
ReplyDeleteGlad you found it useful. I try to put the date on all my posts and all my videos so people can see if it's still worth reading. a 10 year old article on curl or sed is probably still good but most other things change too quickly to get much out of them after a few years :)
Deleteyah… while our server runs Ubuntu 14.04 I need to translate systemd-config to upstart-config for all services…
Delete