본문 바로가기
IT Knowledge/기타

CentOS 부팅시 서비스 자동 시작 등록(ex. Tomcat)

by Seok. 2021. 3. 11.
반응형

Redhat 계열 CentOS Apache-Tomcat init.d 서비스로 등록해서, 부팅시 자동으로 시작되게 하는 방법 정리.

JAVA Apache-Tomcat 제대로 설치되어 있는 상태.

 

[init.d] 등록

경로 : /etc/init.d/jenkins-web 으로 등록

(chkconfig 등록하려면, /etc/init.d 스크립트가 등록되어 있어야 한다. )

 

확인사항 :

CATALINA_HOME : (tomcat 설치되어 있는 경로)

TOMCAT_USER : (tomcat 실행시킬 계정

 

#!/bin/bash

#

# chkconfig: 345 96 30

# description:  Start up the Jenkins Apache-Tomcat

#

# processname: java

# pidfile: /var/run/jenkins-web.pid

#

### BEGIN INIT INFO

# Provides: tomcat

# Required-Start: $network $syslog

# Required-Stop: $network $syslog

# Should-Start: distcache

# Short-Description: start and stop Apache HTTP Server

# Description:

## END INIT INFO

 

# Source function library.

. /etc/init.d/functions

 

## tomcat installation directory

PROCESS_NAME=jenkins-web

 

CATALINA_HOME="/home/jenkins/www/apache-tomcat-8.5.11/"

 

## run as a diffent user

TOMCAT_USER=jenkins

 

##  Path to the pid, runnning info file

pidfile=${PIDFILE-/var/run/${PROCESS_NAME}.pid};

lockfile=${LOCKFILE-/var/lock/subsys/${PROCESS_NAME}};

 

RETVAL=0

 

case "$1" in

 start)

        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`

        if [[ (-n ${PID}) && ($PID -gt 0) ]]; then

                logger -s "${PROCESS_NAME}(pid ${PID}) is  already running."

                exit;

        fi

        if [ -f $CATALINA_HOME/bin/startup.sh ];

          then

            logger -s "Starting Tomcat"

            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/startup.sh -Dprocessname=${PROCESS_NAME}"

            PID=`ps -eaf|grep processname=${PROCESS_NAME}|grep -v grep|awk '{print $2}'`

            RETVAL=$?

            [ $RETVAL = 0 ] && touch ${lockfile}

            [ $RETVAL = 0 ] && echo "${PID}" > ${pidfile}

        fi

        ;;

 stop)

        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`

        ## if PID valid run shutdown.sh

        if [[ -z ${PID} ]];then

            logger -s "${PROCESS_NAME} is not running."

            exit;

        fi

 

        if [[ (${PID} -gt 0) && (-f $CATALINA_HOME/bin/shutdown.sh) ]];

          then

            logger -s "Stopping Tomcat"

            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/shutdown.sh"

            RETVAL=$?

            [ $RETVAL = 0 ] && rm -f ${lockfile}

            [ $RETVAL = 0 ] && rm -f ${pidfile}

        fi

        ;;

 status)

        status -p ${pidfile} ${PROCESS_NAME}

        RETVAL=$?

        ;;

 restart)

         $0 stop

         $0 start

         ;;

version)

        if [ -f $CATALINA_HOME/bin/version.sh ];

          then

            logger -s "Display Tomcat Version"

            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/version.sh"

            RETVAL=$?

        fi

        ;;

 *)

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

        exit 1

        ;;

esac

exit $RETVAL       

 

 

작성한 스크립트에 실행 속성 부여

chmod +x /etc/init.d/jenkins-web

 

 

 

[run-level 등록]

부팅과 동시에 실행하려면 chkconfig 등록해야 한다.

등록

chkconfig --add jenkins-web

등록 확인

$ chkconfig --list jenkins-web

tomcat          0:off   1:off   2:off   3:on    4:on    5:on    6:off

 

이제 부팅시 자동 실행/종료 된다.

 

 

[Commend 자동실행 방법]

$ service tomcat-webapp1  (start / restart / stop / status)

$  service jenkins-web restart

jenkins: Stopping Tomcat

Using CATALINA_BASE:   /home/jenkins/www/apache-tomcat-8.5.11

Using CATALINA_HOME:   /home/jenkins/www/apache-tomcat-8.5.11

Using CATALINA_TMPDIR: /home/jenkins/www/apache-tomcat-8.5.11/temp

Using JRE_HOME:        /usr/java/jdk1.8.0_91

Using CLASSPATH:       /home/jenkins/www/apache-tomcat-8.5.11/bin/bootstrap.jar:/home/jenkins/www/apache-tomcat-8.5.11/bin/tomcat-juli.jar

jenkins: Starting Tomcat

Using CATALINA_BASE:   /home/jenkins/www/apache-tomcat-8.5.11

Using CATALINA_HOME:   /home/jenkins/www/apache-tomcat-8.5.11

Using CATALINA_TMPDIR: /home/jenkins/www/apache-tomcat-8.5.11/temp

Using JRE_HOME:        /usr/java/jdk1.8.0_91

Using CLASSPATH:       /home/jenkins/www/apache-tomcat-8.5.11/bin/bootstrap.jar:/home/jenkins/www/apache-tomcat-8.5.11/bin/tomcat-juli.jar

Tomcat started.

 

lesstif: Stopping Tomcat

Using CATALINA_BASE:   /opt/apache-tomcat-7.0.54

Using CATALINA_HOME:   /opt/apache-tomcat-7.0.54

Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.54/temp

Using JRE_HOME:        /usr

Using CLASSPATH:       /opt/apache-tomcat-7.0.54/bin/bootstrap.jar:/opt/apache-tomcat-7.0.54/bin/tomcat-juli.jar

lesstif: Starting Tomcat

Using CATALINA_BASE:   /opt/apache-tomcat-7.0.54

Using CATALINA_HOME:   /opt/apache-tomcat-7.0.54

Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.54/temp

Using JRE_HOME:        /usr

Using CLASSPATH:       /opt/apache-tomcat-7.0.54/bin/bootstrap.jar:/opt/apache-tomcat-7.0.54/bin/tomcat-juli.jar

Tomcat started.

반응형

'IT Knowledge > 기타' 카테고리의 다른 글

jdbc Oracle SID,SERVICENAME 설정 방법  (0) 2021.05.24
CentOS7 서비스(service) 등록  (1) 2021.04.28
Disk 파티션(GPT vs MBR)  (0) 2021.03.10
Helm 설치하기(Helm 3 Windows Linux)  (0) 2021.01.18
Helm 개념과 구조 기술조사  (0) 2021.01.14

댓글