CentOS7 Linux Tomcat7 자동시작

■ 참고 : CentOS 에 tomcat 을 service 로 등록하여 booting 시 자동으로 구동되게 하기(펌)

■ /etc/rc.d/init.d 폴더에 tomcat7 이라는 파일명으로 시작 스크립트 등록하고 스크립트의 내용중에서 톰캣이 설치된 폴더 위치와 톰캣 실행 사용자를 적절한 일반 사용자로 수정한다.
# vi /etc/rc.d/init.d/tomcat7

#!/bin/bash
#
# tomcat
#
# chkconfig: 345 96 30
# description:  Start up the Tomcat servlet engine.
#
# processname: java
# pidfile: /var/run/tomcat.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: implementation for Servlet 2.5 and JSP 2.1
## END INIT INFO
 
# Source function library.
. /etc/rc.d/init.d/functions
 
## tomcat installation directory
PROCESS_NAME=tomcat

# tomcat이 설치된 폴더 
CATALINA_HOME="/usr/local/tomcat/"
 
## run as a diffent user
# tomcat 실행 사용자를 root가 아닌 일반 사용자로 변경
TOMCAT_USER=nextman
 
##  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	

■ 새로 생성한 /etc/rc.d/init.d/tomcat7 파일에 실행속성을 추가한다.
# chmod +x /etc/rc.d/init.d/tomcat7

■ 부팅과 동시에 tomcat이 실행되게 하려면 run-level에 등록한다.
# chkconfig –add tomcat7

■ run-level에 등록되었는지 확인(run-level이 3,4,5가 ‘on’일 경우 부팅과 동시에 실행된다.)
# chkconfig –list tomcat7

■ service 명령어로 shell에서 실행되는지 확인한다.
# systemctl start tomcat7.service

■ 아래와 같은 메세지와 함께 tomcat 서비스가 실행되지 않으면 다음의 명령어로 실행되지 않는 이유를 확인한다.
Job for tomcat7.service failed. See ‘systemctl status tomcat7.service’ and ‘journalctl -xn’ for details.
# systemctl start tomcat7.service
tomcat7.service – LSB: start and stop Apache HTTP Server
Loaded: loaded (/etc/rc.d/init.d/tomcat7)
Active: failed (Result: resources) since 수 2015-04-08 22:02:05 KST; 6min ago
Process: 2078 ExecStart=/etc/rc.d/init.d/tomcat7 start (code=exited, status=0/SUCCESS)

4월 08 22:02:05 centos01 systemd[1]: Starting LSB: start and stop Apache HTTP Server…
4월 08 22:02:05 centos01 tomcat7[2078]: logger: Starting Tomcat
4월 08 22:02:05 centos01 su[2081]: (to nextman) root on none
4월 08 22:02:05 centos01 tomcat7[2078]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
4월 08 22:02:05 centos01 tomcat7[2078]: At least one of these environment variable is needed to run this program
4월 08 22:02:05 centos01 systemd[1]: PID file /var/run/tomcat.pid not readable (yet?) after start.
4월 08 22:02:05 centos01 systemd[1]: Failed to start LSB: start and stop Apache HTTP Server.
4월 08 22:02:05 centos01 systemd[1]: Unit tomcat7.service entered failed state

■ 위의 메세지는 nextman 사용자의 홈디렉토리의 .bash_profile에 JAVA_HOME환경변수가 설정되지 않은 이유로 tomcat서비스가 실행되지 않았다. nextman사용자의 환경변수에 java(JAVA_HOME)을 적어준다.
# su – nextman
$ vi ~/.bash_profile

■ nextman 사용자의 .bash_profile 내용

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin

export PATH

■ 다시 root사용자로 돌아와서 tomcat 서비스를 실행시킨다.
# systemctl start tomcat7.service
Job for tomcat7.service failed. See ‘systemctl status tomcat7.service’ and ‘journalctl -xn’ for details.
[root@centos01 bin]# systemctl status tomcat7.service
tomcat7.service – LSB: start and stop Apache HTTP Server
Loaded: loaded (/etc/rc.d/init.d/tomcat7)
Active: failed (Result: resources) since 수 2015-04-08 22:18:41 KST; 8s ago
Process: 2217 ExecStart=/etc/rc.d/init.d/tomcat7 start (code=exited, status=0/SUCCESS)

4월 08 22:18:41 centos01 systemd[1]: Starting LSB: start and stop Apache HTTP Server…
4월 08 22:18:41 centos01 tomcat7[2217]: logger: Starting Tomcat
4월 08 22:18:41 centos01 su[2220]: (to nextman) root on none
4월 08 22:18:41 centos01 tomcat7[2217]: touch: cannot touch `/usr/local/tomcat/logs/catalina.out’: 허가 거부
4월 08 22:18:41 centos01 tomcat7[2217]: /usr/local/tomcat//bin/catalina.sh: line 385: /usr/local/tomcat/logs/catalina.out… 거부
4월 08 22:18:41 centos01 systemd[1]: PID file /var/run/tomcat.pid not readable (yet?) after start.
4월 08 22:18:41 centos01 systemd[1]: Failed to start LSB: start and stop Apache HTTP Server.
4월 08 22:18:41 centos01 systemd[1]: Unit tomcat7.service entered failed state.
Hint: Some lines were ellipsized, use -l to show in full.

■ 위의 메세지는 기존에 root사용자로 실행시킬때의 /usr/local/tomcat/logs/catalina.out 로그파일이 root사용자의 권한으로 만들어져서이다. /usr/local/tomcat/ 폴더 하위 폴더및 파일 전부를 nextman사용자.nextman그룹 소유로 변경시키고 다시 tomcat 서비스를 시작한다.
# chown -R nextman.nextman /usr/local/tomcat/
# systemctl start tomcat7.service

■ systemctl start tomcat7.service 명령어로 tomcat 서비스를 시작시키고 나서 아무런 메세지도 없이 # 프롬프트가 떨어지면 정상적으로 tomcat 서비스가 실행된 것이다. 이제 CentOS7 서버를 reboot명령어로 재시작했을 때 tomcat 서비스가 시작 되는지 확인한다.
# reboot
# ps ax | grep tomcat

■ 끝

 

 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다