Creating a linux service for an executable JAR
We need an executable JAR to run as a service on ubuntu linux.
For this we used Java Service Wrapper.
Here a simple example of the use of Java Service Wrapper.
I have build a simple java executable jar with code below.
package executable;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private void messageLoop(String msg){
while(true) {
Date nu = new Date();
String strDate = sdf.format(nu);
LOGGER.info(strDate + " : " + msg);
try
{ Thread.sleep(10000); }
catch (InterruptedException ex)
{ return; }
}
}
public static void main(String[] args) {
LOGGER.info("Start main" );
Main mainKlasse = new Main();
mainKlasse.messageLoop("Hier ben ik ;o)");
LOGGER.info("Einde main" );
}
}
Create an user on linux for the service.
useradd jarserviceuser
mkdir /home/jarserviceuser
chown jarserviceuser.jarserviceuser /home/jarserviceuser
vim /etc/passwd (jarserviceuser:x:1004:1004::/home/jarserviceuser:/bin/bash)
passwd jarserviceuser ( and an appropiate password )
Directories for the new executable-jar application are created, see statemenst below.
mkdir /opt/jarapplication
mkdir /opt/jarapplication/lib
mkdir /opt/jarapplication/bin
mkdir /opt/jarapplication/conf
mkdir /opt/jarapplication/logs
mkdir /opt/jarapplication/tmp
Copy the executable jar to the application directory.
mv executablejar.jar /opt/jarapplication/lib/executablejar.jar
For configuring an executable jar as a service, the Java Service Wrapper is used.
( http://wrapper.tanukisoftware.com/doc/english/download.jsp )
Below the statemens used for configuring the service wrapper.
cd /opt/jarapplication
wget http://wrapper.tanukisoftware.com/download/3.5.26/wrapper-linux-x86-64-3.5.26.tar.gz
gunzip wrapper-linux-x86-64-3.5.26.tar.gz
tar -xvf wrapper-linux-x86-64-3.5.26.tar
cp ./wrapper-linux-x86-64-3.5.26/bin/testwrapper ./bin/
cp ./wrapper-linux-x86-64-3.5.26/bin/wrapper ./bin/
cp ./wrapper-linux-x86-64-3.5.26/lib/* ./lib/
Create a config file and edit this with the right information.
cp ./wrapper-linux-x86-64-3.5.26/src/conf/wrapper.conf.in /opt/jarapplication/conf/wrapper.conf
vim /opt/jarapplication/conf/wrapper.conf
# needed starting from 1
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/executablejar.jar
# Java Additional Parameters
#wrapper.java.additional.1=
wrapper.app.parameter.1=executable.Main
# Application parameters. Add parameters as needed starting from 1
wrapper.logfile=/var/log/jarapplication/jarapplication.log
# Title to use when running as a console
wrapper.console.title=jarapplication
Create a startup script in etc/init.d
cp ./wrapper-linux-x86-64-3.5.26/src/bin/sh.script.in /etc/init.d/jarapplication
vim /etc/init.d/jarapplication
APP_NAME=jarapplication
APP_LONG_NAME="test executable jar"
WRAPPER_CMD="/opt/jarapplication/bin/wrapper"
WRAPPER_CONF="/opt/jarapplication/conf/wrapper.conf"
PIDDIR="/opt/jarapplication/bin"
RUN_AS_USER=jarserviceuser
chmod 0755 /etc/init.d/jarapplication
get the initlevels right with sysv-rc-conf
Make sure the ownerships, rights and folders are correct.
chmod 0644 /opt/jarapplication/lib/executablejar.jar
chown -R jarserviceuser.jarserviceuser /opt/jarapplication/
mkdir /var/log/jarapplication
chown jarserviceuser.jarserviceuser /var/log/jarapplication/
touch /var/log/jarapplication/jarapplication.log
chown jarserviceuser.jarserviceuser /var/log/jarapplication/jarapplication.log
The service is now available with the service command.
service jarapplication status
service jarapplication help
service jarapplication start
service jarapplication stop
Some extra information on creating a service with java service wrapper can be found here (Jenkins example)
For configuring the LOG4J logging, the LOG4J property file has to be configured in the wrapper.conf.
Following line has to be inserted.
wrapper.java.additional.1=-Dlog4j.configuration=file:/opt/jarapplication/conf/log4j.properties
log4j.rootLogger = INFO, STDOUT
#log4j.rootLogger = DEBUG, STDOUT
log4j.appender.STDOUT = org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout = org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern = %5p [%t] (%F:%L) - %m%n
alternative link download