使用systemd 注册tomcat为服务 结果直接爆炸了

Apple
  • 321

RT.

项目背景:需要将项目中的tomcat部署的服务(大概7个),一个springboot jar服务使用脚本管理起来,需要完成的功能 一键启动所有 一键关停所有 一键启动/关停部分 打印日志等等.

最开始参考了docker-compose 的 up和 down命令 自己写了一个比较烧脑的脚本 在/etc/profile中alias一个命令 判断 传入的三个参数 up down logs 根据参数的个数来判断启动/关停的服务

脚本如下

#!/bin/bash

RED_COLOR='\E[1;31m'
YELOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
GREEN_COLOR='\E[1;32m'
RESET='\E[0m'

ECHO_RED="echo -e  ${RED_COLOR}"
ECHO_YESSOW="echo -e  ${YELOW_COLOR}"
ECHO_BLUE="echo -e  ${BLUE_COLOR}"
ECHO_GREEN="echo -e  ${GREEN_COLOR}"
ECHO_WHITE="echo -e  ${RESET}"

WARN_1="please input param such as up or down or logs......"

echoWarnMessage() {
  $ECHO_BLUE "Please enter a correct service name such as $1 $2"
}

isServerHaveStarted() {
  local param1=$1
  IAM_PID=$(lsof -i:"${psMap[$param1]}" | grep -v 'PID' | awk '{print $2}')
  if [ -n "$IAM_PID" ]; then
    echo -e "The service  ${GREEN_COLOR} $param1  ${RESET}has been started. Do not start it again"
    return "233"
  fi
}

declare -A psMap
declare -A execMap
declare -A logMap
psMap=(["bam"]="8081"
  ["bim"]="8080"
  ["idp"]="8082"
  ["apphub"]="8083"
  ["epass"]="8085"
  ["bim-console"]="8084"
  ["cs"]="8759")

execMap=(["bam"]="/data01/tomcat/bam/apache-tomcat-8.5.51/bin/startup.sh"
  ["bim"]="/data01/tomcat/bim/apache-tomcat-8.5.51/bin/startup.sh"
  ["idp"]="/data01/tomcat/idp/apache-tomcat-8.5.51/bin/startup.sh"
  ["apphub"]="/data01/tomcat/apphub/apache-tomcat-8.5.51/bin/startup.sh"
  ["epass"]="/data01/tomcat/epass/apache-tomcat-8.5.51/bin/startup.sh"
  ["bim-console"]="/data01/tomcat/config/bcc/bim-configuration/nohup-run.sh"
  ["cs"]="/data01/tomcat/ConnectorServer/bin/startup.sh /run")

logMap=(["bam"]="/data01/tomcat/bam/apache-tomcat-8.5.51/logs/catalina.out"
  ["bim"]="/data01/tomcat/bim/apache-tomcat-8.5.51/logs/catalina.out"
  ["idp"]="/data01/tomcat/idp/apache-tomcat-8.5.51/logs/catalina.out"
  ["apphub"]="/data01/tomcat/apphub/apache-tomcat-8.5.51/logs/catalina.out"
  ["epass"]="/data01/tomcat/epass/apache-tomcat-8.5.51/logs/catalina.out"
  ["bim-console"]="/data01/tomcat/config/bcc/bim-configuration/bim-configuration.log"
  ["cs"]="/data01/tomcat/ConnectorServer/logs/ConnectorServer.log")

if [ $# -eq 0 ]; then
  echo
  $ECHO_RED "$WARN_1"
  echo
  exit
else
  if [ $# -eq 1 ]; then
    if [ "$1" == 'up' ]; then
      for execkey in ${!execMap[*]}; do
        $ECHO_WHITE
        isServerHaveStarted "$execkey"
        if [ $? -eq "233" ]; then
          continue
        fi
        ${execMap[$execkey]}
        sleep 1.5
        $ECHO_GREEN "$execkey has been started!"
      done
    elif [ "$1" == 'down' ]; then
      for key in ${!psMap[*]}; do
        netstat -nlp | grep :"${psMap[$key]}" | awk '{print $7}' | awk -F"/" '{ print $1 }' | xargs kill -9
        $ECHO_GREEN "$key has been stoped!"
      done
      ${ECHO_GREEN} 'all java process has been stopped!'
    else
      $ECHO_RED "$WARN_1"
    fi
  elif [ "$#" -gt 1 ] && [ "$1" == 'up' ]; then
    runServer=${@:2:$#}
    for sname in "${runServer[@]}"; do
      servercmd=${execMap[$sname]}
      if [ ! "$servercmd" ]; then
        $ECHO_YESSOW "The service $sname does not exist. Skip startup!"
      else
        $ECHO_WHITE
        $servercmd
        sleep 1.5
        $ECHO_GREEN "$sname" "started!"
      fi
    done
  elif [ "$#" -gt 1 ] && [ "$1" == 'down' ]; then
    runServer=${@:2:$#}
    for key in "${runServer[@]}"; do
      if [ ! "${psMap[$key]}" ]; then
        $ECHO_YESSOW "The service $key does not exist. Skip startup!"
        echoWarnMessage "${RED_COLOR}" "${!psMap[*]}"
      else
        $ECHO_WHITE
        netstat -nlp | grep :"${psMap[$key]}" | awk '{print $7}' | awk -F"/" '{ print $1 }' | xargs kill -9
        $ECHO_GREEN "$key has been stoped!"
      fi
    done
  elif [ "$#" -eq 2 ] && [ "$1" == 'logs' ]; then
    p2=${logMap[$2]}
    if [ ! "$p2" ]; then
      $ECHO_YESSOW $key'please input correct server name!'
    else
      tail -f $p2
    fi
  else
    $ECHO_RED "$WARN_1"
  fi
fi

乍一看有点烧脑,其实就是嵌套if else 然后判断传入的参数 来判断服务的状态 以及启动的个数,启动时直接调用tocmat的startup.sh 关停则是直接用kill -9 根据端口来强制删除(因为有个别服务 会启动很多后台进程 直接调用shutdown.sh一次还关停不了)

后来又同学说着太复杂了 脚本烧脑 不好维护

建议用systemd来管理
于是我分别试了一下 initd的方式和systemd

  1. 在系统/etc/init.d 目录下创建文件bam并编辑如下内容

修改其中对应的变量 tomcat_home startup shutdown等

#!/bin/bash  
# This is the init script for starting up the  
#  Jakarta Tomcat server  
#  
# chkconfig: 345 91 10  
# description: Starts and stops the Tomcat daemon.  
#  

# Source function library.  
# . /etc/rc.d/init.d/functions  

# Get config.  
# . /etc/sysconfig/network  

# Check that networking is up.  
# [ "${NETWORKING}" = "no" ] && exit 0  

export JAVA_HOME=/data01/JRE/jdk1.8.0_271 
tomcat_home=/data01/tomcat/bam/apache-tomcat-8.5.51
startup=$tomcat_home/bin/startup.sh  
shutdown=$tomcat_home/bin/shutdown.sh  

start(){  
   echo -n "Starting bam service:"  
   cd $tomcat_home  
   $startup  
   echo "bam is succeessfully started up"  
}  

stop(){  
   echo -n "Shutting down tomcat: "  
   cd $tomcat_home  
   $shutdown  
   echo "tomcat is succeessfully shut down."  
}  



status(){  
    numproc=$(ps -ef | grep bam | grep -v "grep bam" | wc -l)  
    if [ $numproc -gt 0 ]; then  
       echo "bam is running..."  
    else  
       echo "bam is stopped..."  
    fi  
}  



restart(){  
   stop  
   start  
}  

# See how we were called.  
case "$1" in  
start)  
   start  
   ;;  
stop)  
   stop  
   ;;  
status)  
   status  
   ;;  
restart)  
   restart  
   ;;  
*)  
   echo $"Usage: $0 {start|stop|status|restart}"  
   exit 1  
esac

保存好文件,并赋权 chmod +x bam

  1. 添加bam服务
chkconfig --add bam
# 开机启动tomcat服务
chkconfig bam on  
service bam start/stop/restart #即可启动服务
#如果启动说找不到目标service 之类的话可以 执行下行命令接触屏蔽
systemctl unmask bam

发现这样是可以用

systemctl start/stop/status 来管理的
  1. 使用systemd来管理 tomcat服务

编辑 /usr/lib/systemd/system 下的idp文件 内容如下

[Unit]
Description=tomcat service idp

[Service]
Type=simple
ExecStart=/data01/tomcat/idp/apache-tomcat-8.5.51/bin/startup.sh
ExecStop=/data01/tomcat/idp/apache-tomcat-8.5.51/bin/shutdown.sh
KillMode=control-group
Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

执行systemctl daemon-reload

执行systemctl start idp 命令

后台日志如下

image.png

what fuck?

明明是启动,不知道为啥启动的一瞬间又调用了shutdown脚本
下面是tomcat的bin目录下的文件

目前问题有2:

  1. systemd启动service直接报错
  2. 如何把这些服务加到一个group里面 一键启动或者关停?

下午起来又翻了一篇博文

CentOS7下利用systemd机制实现tomcat开机自启动

参照这篇博文的配置 我对应自己的修改了一下 然后就可以了

[Unit]
Description=Tomcat8
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking

#PIDFile=/data01/tomcat/bim/apache-tomcat-8.5.51/bin/tomcat.pid
ExecStart=/data01/tomcat/bim/apache-tomcat-8.5.51/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
KillMode=control-group
Restart=no

[Install]
WantedBy=multi-user.target

使用target来管理服务 添加了 Wants 和Stopedwhennoneeded

独独就是有一个服务没停止也是醉了

image.png
image.png

Systemd 添加自定义服务(开机自启动)

回复
阅读 1.4k
2 个回答

第一个问题是type的问题

第二个可以写一个target,把需要一起启动的service都加到target.wants里面。
然后启动就是 systemctl start all_tomcat.target
停止就是 systemctl stop all_tomcat.target

停止的时候会出现停止不了的情况,还要在service配置文件里面加上一个 StopWhenUnneeded=yes

Apple
  • 321
宣传栏