JavaServiceWrapper
The Java Service Wrapper enables a Java Application to be run as a Windows Service or UNIX Daemon. It also monitors the health of your Application and JVM. http://wrapper.tanukisoftware.com/doc/english/product-overview.html
Community Editions:
wget http://wrapper.tanukisoftware.com/download/3.5.25/wrapper-linux-x86-32-3.5.25.tar.gz , 32 bit
wget http://wrapper.tanukisoftware.com/download/3.5.25/wrapper-linux-x86-64-3.5.25.tar.gz , 64 bit
Integrate with other apps
http://wrapper.tanukisoftware.com/doc/english/integrate.html
http://wrapper.tanukisoftware.com/doc/english/integrate-simple-nix.html
Sample Maven App to integrate
Maven project structure
. |-- pom.xml |-- src | `-- main | |-- java | | `-- org | | `-- allowed | | `-- bitarus | | `-- JavaServiceApp.java | `-- resources | `-- simplelogger.properties `-- target |-- archive-tmp |-- classes | |-- org | | `-- allowed | | `-- bitarus | | `-- JavaServiceApp.class | `-- simplelogger.properties |-- javaServiceApp-0.0.1-jar-with-dependencies.jar |-- javaServiceApp-0.0.1.jar |-- maven-archiver | `-- pom.properties `-- surefire 15 directories, 8 files
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3 http://maven.apache.org/maven-v4_0_0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>org.allowed.bitarus</groupId>
6 <artifactId>javaServiceApp</artifactId>
7 <packaging>jar</packaging>
8 <version>0.0.1</version>
9 <name>javaServiceApp</name>
10 <url>http://maven.apache.org</url>
11 <build>
12 <plugins>
13 <plugin>
14 <artifactId>maven-assembly-plugin</artifactId>
15 <version>2.4</version>
16 <configuration>
17 <descriptorRefs>
18 <descriptorRef>jar-with-dependencies</descriptorRef>
19 </descriptorRefs>
20 <archive>
21 <manifest>
22 <mainClass>org.allowed.bitarus.JavaServiceApp</mainClass>
23 </manifest>
24 </archive>
25 </configuration>
26 <executions>
27 <execution>
28 <id>make-assembly</id>
29 <phase>package</phase>
30 <goals>
31 <goal>single</goal>
32 </goals>
33 </execution>
34 </executions>
35 </plugin>
36 </plugins>
37 </build>
38 <dependencies>
39 <dependency>
40 <groupId>org.slf4j</groupId>
41 <artifactId>slf4j-api</artifactId>
42 <version>1.7.7</version>
43 </dependency>
44 <dependency>
45 <groupId>org.slf4j</groupId>
46 <artifactId>slf4j-simple</artifactId>
47 <version>1.7.7</version>
48 </dependency>
49 </dependencies>
50 </project>
src/main/resources/simplelogger.properties
org.slf4j.simpleLogger.logFile=/tmp/JavaServiceApp.log org.slf4j.simpleLogger.defaultLogLevel=debug org.slf4j.simpleLogger.showDateTime=true org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z
src/main/java/org/allowed/bitarus/JavaServiceApp.java
1 package org.allowed.bitarus;
2
3 public class JavaServiceApp{
4 public static void main(String [] args){
5 org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JavaServiceApp.class);
6
7 while(true){
8 try{
9 log.debug("Test JavaServiceApp");
10 Thread.sleep(10000);
11 }
12 catch(Exception ex){
13 log.error( ex.getMessage() );
14 }
15 }
16 }
17 }
Integration with JavaServiceWrapper
1 adduser jsa
2 cp target/javaServiceApp-0.0.1-jar-with-dependencies.jar /home/jsa/
3 cp /tmp/wrapper-linux-x86-32-3.5.25/lib/wrapper.jar /home/jsa
4 cp /tmp/wrapper-linux-x86-32-3.5.25/src/conf/wrapper.conf.in /home/jsa/wrapper.conf
5 cp /tmp/wrapper-linux-x86-32-3.5.25/src/bin/sh.script.in /etc/init.d/jsa
6 chmod 755 /etc/init.d/jsa
7 cp /tmp/wrapper-linux-x86-32-3.5.25/bin/wrapper /usr/bin/wrapper
8 cp /tmp/wrapper-linux-x86-32-3.5.25/lib/libwrapper.so /usr/lib/libwrapper.so
/etc/init.d/jsa , change APP and WRAPPER
# Application APP_NAME="JavaServiceApp" APP_LONG_NAME="Java Service App" # Wrapper WRAPPER_CMD="/usr/bin/wrapper" WRAPPER_CONF="/home/jsa/wrapper.conf" PIDDIR="/home/jsa" RUN_AS_USER=jsa
/home/jsa structure
. |-- javaServiceApp-0.0.1-jar-with-dependencies.jar |-- wrapper.conf |-- wrapper.jar `-- wrapper.log
/home/jsa/wrapper.conf
#encoding=UTF-8 wrapper.lang.folder=/home/jsa/lang wrapper.java.command=/opt/java/bin/java set.JAVA_HOME=/opt/java wrapper.java.command=%JAVA_HOME%/bin/java wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp wrapper.java.classpath.1=/home/jsa/wrapper.jar wrapper.java.classpath.2=/home/jsa/javaServiceApp-0.0.1-jar-with-dependencies.jar wrapper.java.library.path.1=/usr/lib wrapper.java.additional.auto_bits=TRUE wrapper.java.additional.1= wrapper.app.parameter.1=org.allowed.bitarus.JavaServiceApp wrapper.console.format=PM wrapper.console.loglevel=INFO wrapper.logfile=/home/jsa/wrapper.log wrapper.logfile.format=LPTM wrapper.logfile.loglevel=INFO wrapper.logfile.maxsize=0 wrapper.logfile.maxfiles=0 wrapper.syslog.loglevel=NONE wrapper.ignore_sequence_gaps=TRUE wrapper.pidfile.strict=TRUE wrapper.console.title=@app.long.name@ wrapper.check.deadlock=TRUE wrapper.check.deadlock.interval=60 wrapper.check.deadlock.action=RESTART wrapper.check.deadlock.output=FULL wrapper.filter.trigger.1000=[Loaded java.lang.OutOfMemoryError wrapper.filter.action.1000=NONE wrapper.filter.trigger.1001=java.lang.OutOfMemoryError wrapper.filter.action.1001=RESTART wrapper.filter.message.1001=The JVM has run out of memory. wrapper.event.jvm_restart.email.body=The JVM was restarted.\n\nPlease check on its status.\n wrapper.name=@app.name@ wrapper.displayname=@app.long.name@ wrapper.description=@app.description@ wrapper.ntservice.dependency.1= wrapper.ntservice.starttype=AUTO_START wrapper.ntservice.interactive=false
Start service, /etc/init.d/jsa start . The wrapper and service should be running under jsa account.
Stop service, /etc/init.d/jsa stop .
Init script without using the Java Service Wrapper
This script does not require the JavaServiceWrapper and probably will run on all Linux distributions.
/etc/init.d/jsa2
1 # chkconfig: 345 99 99
2 # description: Starts and stops the jsa process
3 USER=jsa
4 PROG=jsa
5 PIDFILE="/tmp/jsa.pid"
6 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin"
7 LOG="/tmp/jsa.log"
8 JAVA="/opt/java/bin/java"
9 JARFILE="/home/jsa/javaServiceApp-0.0.1-jar-with-dependencies.jar"
10
11 start(){
12 echo "$PROG started with user $USER"
13 su - $USER -c "$JAVA -jar $JARFILE " >> $LOG &
14 /bin/sleep 2
15 PID=`ps uax | grep java | grep $PROG | grep $USER | head -n 1 | awk '{print $2}'`
16 echo "$PROG running with PID: $PID"
17 echo "$PID" > $PIDFILE
18 }
19
20 status(){
21 PID=`cat $PIDFILE`
22 echo "Running $PROG with PID: $PID"
23 }
24
25 stop(){
26 echo "Stop called for $PROG"
27 PID=`cat $PIDFILE`
28 echo "PID to kill $PROG $PID"
29 kill -9 $PID
30 rm $PIDFILE
31 }
32
33 # switch
34 case "$1" in
35 start)
36 start
37 ;;
38 status)
39 status
40 ;;
41 stop)
42 stop
43 ;;
44 restart)
45 stop
46 start
47 ;;
48 *)
49 echo "$PROG start|status|stop|restart"
50 exit 1
51 ;;
52 esac