Thursday, 3 November 2016

How to create a Java (/Clojure/Scala) service on Linux

This is a sort of a continuation of an earlier post. In that post I said that you have to create a shell file called test-service or whatever in /etc/init.d/.

You will need Apache Jsvc. Your Java (or whatever JVM language) code will include a class that implements org.apache.commons.daemon.Daemon.

Here is what you should put in that shell file to make you JVM thing into a Linux service called test-service (test.daemon.Daemon is the class you wrote which implements org.apache.commons.daemon.Daemon).

#! /bin/sh                                                                                                                                                                                     
### BEGIN INIT INFO                                                                                                                                                                            
# Provides:     test-service                                                                                                                                                                   
# Required-Start:                                                                                                                                                                              
# Required-Stop:                                                                                                                                                                               
# Default-Start:     S                                                                                                                                                                         
# Default-Stop:                                                                                                                                                                                
# X-Start-Before:                                                                                                                                                                              
# Short-Description:                                                                                                                                                                           
# Description: test-service                                                                                                                                                                    
### END INIT INFO                                                                                                                                                                              
                                                                                                                                                                                               
                                                                                                                                                                                               
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64                                                                                                                                             
SVC_DIR=/home/mikey/test-service                                                                                                                                                               
                                                                                                                                                                                               
                                                                                                                                                                                               
case "$1" in                                                                                                                                                                                   
    start|"")                                                                                                                                                                                  
        /usr/bin/jsvc -wait 20 -cwd $SVC_DIR -pidfile $SVC_DIR/test-service.pid -outfile $SVC_DIR/out.log -errfile $SVC_DIR/err.log -cp $SVC_DIR/target/test-service-0.1-standalone.jar test.daemon.Daemon                                                                                                                                                                                   
        exit $?                                                                                                                                                                                
        ;;                                                                                                                                                                                     
    restart|reload|force-reload)                                                                                                                                                               
        echo "Error: argument '$1' not supported" >&2                                                                                                                                          
        exit 3                                                                                                                                                                                 
        ;;                                                                                                                                                                                     
    stop)                                                                                                                                                                                      
        /usr/bin/jsvc -wait 20 -stop -cwd $SVC_DIR -pidfile $SVC_DIR/test-service.pid -outfile $SVC_DIR/out.log -errfile $SVC_DIR/err.log -cp $SVC_DIR/target/test-service-0.1-standalone.jar test.daemon.Daemon                                                                                                                                                                             
        exit $?                                                                                                                                                                                
        ;;                                                                                                                                                                                     
    status)                                                                                                                                                                                    
        # No-op                                                                                                                                                                                
        ;;                                                                                                                                                                                     
    *)                                                                                                                                                                                         
        echo "Usage: test-service.sh [start|stop]" >&2                                                                                                                                         
        exit 3                                                                                                                                                                                 
        ;;                                                                                                                                                                                     
esac                                                                                                                                                                                           
                                                                                                                                                                                               
:    

No comments:

Post a Comment