CentOS - Tomcat 서비스로 등록

분류없음 2017.12.03 03:26 Posted by 파란크리스마스

출처 : RHEL/CentOS tomcat7 init.d service script. · GitHub

tomcat 서비스 스크립트 작성

$ sudo vi /etc/init.d/tomcat

tomcat 내용

#!/bin/sh
#
# /etc/init.d/tomcat7 -- startup script for the Tomcat 7 servlet engine
#
# Written by Miquel van Smoorenburg .
# Modified for Debian GNU/Linux	by Ian Murdock .
# Modified for Tomcat by Stefan Gybas .
# Modified for Tomcat6 by Thierry Carrez .
# Modified for Tomcat7 by Ernesto Hernandez-Novich .
# Additional improvements by Jason Brittain .
#
### BEGIN INIT INFO
# Provides:          tomcat7
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tomcat.
# Description:       Start the Tomcat servlet engine.
# sudo update-rc.d tomcat defaults
# sudo update-rc.d tomcat remove
# ls /etc/rc0.d
### END INIT INFO

set -e

## Source function library.
#. /etc/rc.d/init.d/functions
export PROCESS_NAME=tomcat-real
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-1.b12.el6_9.x86_64
export JAVA_OPTS="-Dfile.encoding=UTF-8 \
  -Dcatalina.logbase=/var/log/tomcat \
  -Dnet.sf.ehcache.skipUpdateCheck=true \
  -XX:+DoEscapeAnalysis \
  -XX:+UseConcMarkSweepGC \
  -XX:+CMSClassUnloadingEnabled \
  -XX:+UseParNewGC \
  -Xms1024m -Xmx2048m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"
export PATH=$JAVA_HOME/bin:$PATH
export TOMCAT_USER=root
export TOMCAT_HOME=/opt/tomcat
export SHUTDOWN_WAIT=20

tomcat_pid() {
  echo `ps aux | grep ${PROCESS_NAME} | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su - $TOMCAT_USER -c "$TOMCAT_HOME/bin/startup.sh -Dprocessname=${PROCESS_NAME}"
  fi


  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stoping Tomcat"
    /bin/su - $TOMCAT_USER -c $TOMCAT_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\nwaiting for processes to exit";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\n"
      kill -9 $pid
    fi
  else
    echo "Tomcat is not running"
  fi
 
  return 0
}

case $1 in
start)
  start
;; 
stop)   
  stop
;; 
restart)
  stop
  start
;;
status)
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
  else
    echo "Tomcat is not running"
  fi
;; 
esac    
exit 0

tomcat 파일 실행권한 부여

$ sudo chmod u+x /etc/init.d/tomcat

tomcat 서비스 등록

$ sudo chkconfig --add tomcat

tomcat 서비스 삭제

$ sudo chkconfig --del tomcat


 

티스토리 툴바