[Fortify] SSC apache-tomcat 시작/종료 관리 스크립트

#!/bin/bash
############################################################
#
# 여러개의 각각 다른 포트를 사용하는 apache-tomcat에 대해
# 시작/종료를 관리한다.
#
############################################################

SSC_ONOFF=("0"          "0"         "1"         "1")
SSC_USERS=("ssc_0431"   "ssc_1620"  "ssc_1910"  "ssc_2010")
SSC_PORTS=("8086"       "8085"      "8084"      "8080")
SSC_HOME_DIR="/home"

############################################################
#
# server.xml에서 기본 8080 포트번호를 +index 만큼 더하여
# 변경한다.
# 예) port_changer 6 /home/ssc_0431/8086/conf/server.xml
#  index=6 > 8086으로 변경됨
#
############################################################
function port_changer () {
  if [ ! -f $2 ]
  then
    echo "$2 not found."
    exit 1
  fi

  if [ ! -f $2.orig ]
  then
    cp -fv $2 $2.orig
  fi

  PORT1=$(expr 8080 + $1)
  PORT2=$(expr 8443 + $1)
  PORT3=$(expr 8009 + $1)
  PORT4=$(expr 8005 + $1)

  if ! [ -w $2 ]; then
    usage
  fi

  echo "> index: $1"
  echo "> file: $2"

  sed -i "s/8080/${PORT1}/g" $2
  sed -i "s/8443/${PORT2}/g" $2
  sed -i "s/8009/${PORT3}/g" $2
  sed -i "s/8005/${PORT4}/g" $2
}

############################################################
#
# 파일 권한 변경 및 로그를 삭제한다.
#
############################################################
function init_daemon () {
  COUNT1=0
  for LOOP1 in "${SSC_USERS[@]}"
  do
    chown -R "${LOOP1}:users" "/home/${LOOP1}"
    chmod 755 /home/${LOOP1}/${SSC_PORTS[${COUNT1}]}/bin/*.sh
    COUNT1=$(expr ${COUNT1} + 1)
  done

  COUNT1=0
  for LOOP1 in "${SSC_USERS[@]}"
  do
    rm -rf /home/${LOOP1}/${SSC_PORTS[${COUNT1}]}/logs/*
    rm -rf /home/${LOOP1}/${SSC_PORTS[${COUNT1}]}/temp/*
    rm -rf /home/${LOOP1}/.fortify/ssc/logs/*
    COUNT1=$(expr ${COUNT1} + 1)
  done
}

case "$1" in
    start)
      echo "Starting apache-tomcat services: "
      COUNT1=0
      for LOOP1 in "${SSC_USERS[@]}"
      do
        if [ ${SSC_ONOFF[${COUNT1}]} == "1" ]
        then
          su - -c "/home/${LOOP1}/${SSC_PORTS[${COUNT1}]}/bin/startup.sh" ${LOOP1}
        fi
        COUNT1=$(expr ${COUNT1} + 1)
      done
      ;;

    stop)
      echo "Shutting down apache-tomcat services: "
      COUNT1=0
      for LOOP1 in "${SSC_USERS[@]}"
      do
        if [ ${SSC_ONOFF[${COUNT1}]} == "1" ]
        then
          su - -c "/home/${LOOP1}/${SSC_PORTS[${COUNT1}]}/bin/shutdown.sh" ${LOOP1}
        fi
        COUNT1=$(expr ${COUNT1} + 1)
      done
      ;;
    restart)
      $0 stop
      $0 start
      ;;
    init)
      init_daemon
      ;;
    pc)
      port_changer $2 $3
      ;;
    *)
      echo "Usage: $0 {start|stop|restart|init|pc}"
      echo "  ex)  $0 pc 6 /home/tomcat/conf/server.xml"
      exit 1
    ;;
esac

위로 스크롤