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 jarserviceusermkdir /home/jarserviceuserchown jarserviceuser.jarserviceuser /home/jarserviceuservim /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/jarapplicationmkdir /opt/jarapplication/libmkdir /opt/jarapplication/binmkdir /opt/jarapplication/confmkdir /opt/jarapplication/logsmkdir /opt/jarapplication/tmpCopy the executable jar to the application directory.
mv executablejar.jar /opt/jarapplication/lib/executablejar.jarFor 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/jarapplicationwget http://wrapper.tanukisoftware.com/download/3.5.26/wrapper-linux-x86-64-3.5.26.tar.gzgunzip wrapper-linux-x86-64-3.5.26.tar.gztar -xvf wrapper-linux-x86-64-3.5.26.tarcp ./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 1wrapper.java.classpath.1=../lib/wrapper.jarwrapper.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 1wrapper.logfile=/var/log/jarapplication/jarapplication.log
# Title to use when running as a consolewrapper.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/jarapplicationvim /etc/init.d/jarapplication
APP_NAME=jarapplicationAPP_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=jarserviceuserchmod 0755 /etc/init.d/jarapplicationget the initlevels right with sysv-rc-conf
Make sure the ownerships, rights and folders are correct.
chmod 0644 /opt/jarapplication/lib/executablejar.jarchown -R jarserviceuser.jarserviceuser /opt/jarapplication/mkdir /var/log/jarapplicationchown jarserviceuser.jarserviceuser /var/log/jarapplication/touch /var/log/jarapplication/jarapplication.logchown jarserviceuser.jarserviceuser /var/log/jarapplication/jarapplication.logThe service is now available with the service command.
service jarapplication statusservice jarapplication helpservice jarapplication startservice jarapplication stopSome 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
The file /opt/jarapplication/conf/log4j.properties can be created with following lines. (This lets you control the LOG4J output)
log4j.rootLogger = INFO, STDOUT#log4j.rootLogger = DEBUG, STDOUTlog4j.appender.STDOUT = org.apache.log4j.ConsoleAppenderlog4j.appender.STDOUT.layout = org.apache.log4j.PatternLayoutlog4j.appender.STDOUT.layout.ConversionPattern = %5p [%t] (%F:%L) - %m%n
{ Read More }