Java Linux Service install

WHORU 리눅스 버전을 만들다 보니 자바로 개발하게 되었습니다.

리눅스용도로 구현해야 하는 부분중에 가장 급한 부분이 성능부분이라고 생각해서 만들게 되었는데, 서비스 형태로 인스톨하는 방법도 중요한 것 같아 먼저 포스팅 합니다.

이 포스팅은 리눅스로 개발된 프로그램에 공통적으로 적용이 가능한 것입니다.

WHORU perf 서비스 기준으로 설명드립니다~

먼저 Oepn Java SDK를 삭제한다

설치 유무 확인

rpm -qa | grep -E ‘^open[jre|jdk]|j[re|dk]’

삭제명령

sudo yum remove java-1.7.0-openjdk

sudo yum remove java-1.6.0-openjdk

그리고 자바 설치 파일 다운로드

http://www.java.com

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

 

그리고 설치 시작

sudo rpm -ivh jdk-8u31-linux-x64.rpm

자바 환경 변수 등록

export JAVA_HOME=/usr/java/jdk1.8.0_31/

export PATH=$PATH:$JAVA_HOME

설치 경로 생성

sudo mkdir /var/whoruperf

복사

sudo cp -r ./com /var/whoruperf

서비스 등록 준비

sudo vi /etc/rc.d/init.d/whoruperf

#!/bin/bash
#
# javaserver: Startup script for Any Server Application.
#
# chkconfig: 35 80 05
# description: Startup script for Any Server Application.

SCRIPT_HOME=/var/whoruperf;

export SCRIPT_HOME

start() {

echo -n "Starting whoru performance service: "

$SCRIPT_HOME/run.sh start

sleep 2

echo "done"

}

stop() {

echo -n "Stopping whoru performance service: "

$SCRIPT_HOME/run.sh stop

echo "done"

}

status() {

echo -n "Checking whoru performance service: "

$SCRIPT_HOME/run.sh status

echo "done"

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

status)

status

;;

restart)

stop

start

;;

*)

echo $"Usage: whoruperf {start|stop|restart|status}"

exit

esac

 

서비스 등록

sudo chkconfig –add whoruperf

서비스 조정 파일 생성

sudo vi /var/whoruperf/run.sh

if [ "$(ps -p $pid --no-headers -o comm)" != "java" ]; then return 1; fi
grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
if [ $? -ne 0 ]; then return 1; fi
return 0; }
# Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
return 0; }
function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo $! >$pidFile"
# Don't forget to add -H so the HOME environment variable will be set correctly.
#sudo -u $serviceUser -H $SHELL -c "$cmd" || return 1
su --session-command="$javaCommandLine >>$serviceLogFile 2>&1 & echo $! >$pidFile" $serviceUser || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "n$serviceName start failed, see logfile."
return 1
fi
return 0; }
function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo -e "n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<killWaitTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
return 1; }
function startService {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
echo -n "Starting $serviceName "
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "started PID=$pid"
RETVAL=0
return 0; }
function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
echo -n "Stopping $serviceName "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0; }
function checkServiceStatus {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0; }
function main {
RETVAL=0
case "$1" in
start) # starts the Java program as a Linux service
startService
;;
stop) # stops the Java program service
stopService
;;
restart) # stops and restarts the service
stopService && startService
;;
status) # displays the service status
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
}

 

실행권한 부여

sudo chmod +x /etc/init.d/whoruperf

sudo chmod +x /var/whoruperf/run.sh

Facebook Comments

Leave A Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.