LINUX
2019.07.16 / 18:42

Linux - Java 데몬 실행(Apache commons daemon, Jsvc)

탁돌이개발자
추천 수 22

# jsvc 설치및 java 데몬 샘플 구동및 중지


참고 자료 :
 http://commons.apache.org/proper/commons-daemon/jsvc.html

jsvc를 이용한 Linux 운영체제에서 java 프로세스를 데몬형식(백그라운드)으로 실행및 중지

샘플 자료 위치 : commons-daemon-1.0.15-src/src/samples


> 디렉토리 생성및 이동

[sunshiny@node01 ~]$ pwd
/home/sunshiny
[sunshiny@node01 ~]$ mkdir commons-daemon
[sunshiny@node01 ~]$ cd commons-daemon


> 데몬 소스및  jar 파일 다운로드

[sunshiny@node01 ]$ wget http://mirror.apache-kr.org//commons/daemon/source/commons-daemon-1.0.15-src.tar.gz
[sunshiny@node01 ]$ wget http://www.apache.org/dist/commons/daemon/binaries/commons-daemon-1.0.15.jar

[sunshiny@node01 ]$ ls
commons-daemon-1.0.15.jar  commons-daemon-1.0.15-src.tar.gz


> 데몬 소스 압축 해제

[sunshiny@node01 ]$ tar -xzvf commons-daemon-1.0.15-src.tar.gz


> 디렉토리 이동

[sunshiny@node01 ]$ cd commons-daemon-1.0.15-src/src/native/unix/
[sunshiny@node01 ]$ ls
CHANGES.txt  configure  configure.in  INSTALL.txt  Makedefs.in  Makefile.in  man  native  support


> buildconf 스크립트 실행(autoconf: command not found 에러 발생시 맨 아래 참조)

[sunshiny@node01 ]$ support/buildconf.sh 
support/buildconf.sh: configure script generated successfully


> java 홈정보를 설정후 시스템 configure 실행

[sunshiny@node01 ]$ ./configure --with-java=/usr/java_home
또는
[sunshiny@node01 ]$ export JAVA_HOME=/usr/java/jdk1.6.0_45
[sunshiny@node01 ]$ ./configure


> jsvc 생성

[sunshiny@node01 ]$ make


> jsvc 조회

[sunshiny@node01 ]$ ls
autom4te.cache  config.nice    configure.in  Makedefs     Makefile.in  support
CHANGES.txt     config.status  INSTALL.txt   Makedefs.in  man
config.log      configure      jsvc          Makefile     native

[sunshiny@node01 ]$ ./jsvc -help
Usage: jsvc [-options] class [args...]

Where options include:

    -help | --help | -?
        show this help page (implies -nodetach)
    -jvm <JVM name>
        use a specific Java Virtual Machine. Available JVMs:
            'server'
    -client
        use a client Java Virtual Machine.
    -server
        use a server Java Virtual Machine.
    -cp | -classpath <directories and zip/jar files>
        set search path for service classes and resouces
    -java-home | -home <directory>
        set the path of your JDK or JRE installation (or set
        the JAVA_HOME environment variable)
    -version
        show the current Java environment version (to check
        correctness of -home and -jvm. Implies -nodetach)
    -showversion
        show the current Java environment version (to check
        correctness of -home and -jvm) and continue execution.
    -nodetach
        don't detach from parent process and become a daemon
    -debug
        verbosely print debugging information
    -check
        only check service (implies -nodetach)
    -user <user>
        user used to run the daemon (defaults to current user)
    -verbose[:class|gc|jni]
        enable verbose output
    -cwd </full/path>
        set working directory to given location (defaults to /)
    -outfile </full/path/to/file>
        Location for output from stdout (defaults to /dev/null)
        Use the value '&2' to simulate '1>&2'
    -errfile </full/path/to/file>
        Location for output from stderr (defaults to /dev/null)
        Use the value '&1' to simulate '2>&1'
    -pidfile </full/path/to/file>
        Location for output from the file containing the pid of jsvc
        (defaults to /var/run/jsvc.pid)
    -D<name>=<value>
        set a Java system property
    -X<option>
        set Virtual Machine specific option
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
        enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
        disable assertions
    -esa | -enablesystemassertions
        enable system assertions
    -dsa | -disablesystemassertions
        disable system assertions
    -agentlib:<libname>[=<options>]
        load native agent library <libname>, e.g. -agentlib:hprof
    -agentpath:<pathname>[=<options>]
        load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
        load Java programming language agent, see java.lang.instrument
    -procname <procname>
        use the specified process name
    -wait <waittime>
        wait waittime seconds for the service to start
        waittime should multiple of 10 (min=10)
    -stop
        stop the service using the file given in the -pidfile option
    -keepstdin
        does not redirect stdin to /dev/null

jsvc (Apache Commons Daemon) 1.0.15-dev
Copyright (c) 1999-2013 Apache Software Foundation.


> jsvc 파일을 사용될 위치로 이동

[sunshiny@node01 ]$ mv jsvc ~/commons-daemon/
[sunshiny@node01 ]$ cd ~/commons-daemon/



# java 데몬 샘플 구동


> commons-daemon-1.0.15-src/src/samples 디렉토리의 샘플 파일 복사

[sunshiny@node01 ]$ cp -r samples ~/commons-daemon/
[sunshiny@node01 ]$ cd ~/commons-daemon/samples/


> 데몬 구동및 중지 스크립트 설정
[sunshiny@node01 ]$ vi SimpleApplication.sh 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Small shell script to show how to start the sample services.
#
# Adapt the following lines to your configuration
JSVC_HOME=/home/sunshiny/commons-daemon      # jsvc 파일 위치 디렉토리
JAVA_HOME=/usr/java/jdk1.6.0_45
PROGRAM=SimpleApplication
CLASSPATH=`pwd`/$PROGRAM.jar:`pwd`/commons-daemon-1.0.15.jar
 
case "$1" in
  start )
    shift
    $JSVC_HOME/jsvc \
        -home $JAVA_HOME \
        -cp $CLASSPATH \
        -nodetach \
        -errfile "&2" \
        -pidfile `pwd`/$PROGRAM.pid \
        @$PROGRAM \
        -start-method main \
        $*
    exit $?
    ;;
  stop )
    shift
    $JSVC_HOME/jsvc \
        -home $JAVA_HOME \
        -cp $CLASSPATH \
        -stop \
        -nodetach \
        -errfile "&2" \
        -pidfile `pwd`/$PROGRAM.pid \
        @$PROGRAM \
        -start-method main \
        $*
    exit $?
    ;;
    * )
    echo 'Usage SimpleApplication.sh start | stop'
    exit 1
    ;;
esac 



> SimpleApplication 컴파일

[sunshiny@node01 ]$ javac -d . SimpleApplication.java
Note: SimpleApplication.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

[sunshiny@node01 ]$ ls SimpleApp* commons*
commons-daemon-1.0.15.jar        SimpleApplication$ShutdownHook.class
SimpleApplication.class          SimpleApplication.java
SimpleApplication$Handler.class  SimpleApplication.sh


> 컴파일된 SimpleApplication.class 파일 모두 jar로 묶음

[sunshiny@node01 ]$ jar cvf SimpleApplication.jar ./SimpleApplication*.class
added manifest
adding: SimpleApplication.class(in = 3901) (out= 2024)(deflated 48%)
adding: SimpleApplication$Handler.class(in = 4431) (out= 2408)(deflated 45%)
adding: SimpleApplication$ShutdownHook.class(in = 773) (out= 491)(deflated 36%)

[sunshiny@node01 ]$ ls SimpleApp* commons*
commons-daemon-1.0.15.jar        SimpleApplication.jar   SimpleApplication$ShutdownHook.class
SimpleApplication.class          SimpleApplication.java
SimpleApplication$Handler.class  SimpleApplication.sh


> 데몬 스크립트 구동및 중지

[sunshiny@node01 ]$ ./SimpleApplication.sh 
Usage SimpleApplication.sh start | stop

[sunshiny@node01 ]$ ./SimpleApplication.sh start
SimpleApplication: instance 2103957824 created
SimpleApplication: instance 2103957824 init 0
SimpleApplication: loading on port 1200
SimpleApplication: starting
SimpleApplication: started acceptor loop

[sunshiny@node01 ]$ ./SimpleApplication.sh stop
SimpleApplication: stopping
SimpleApplication: exiting acceptor loop
SimpleApplication: stopped




> buildconf.sh 에러

[sunshiny@node01 ]$ support/buildconf.sh
support/buildconf.sh: line 25: autoconf: command not found
support/buildconf.sh: cannot generate configure script

> libtool 패키지 설치후 다시 진행
[sunshiny@node01 ]$ yum install libtool