CentOS7 tomcat systemctl脚本

1,centos7 使用 systemctl 替换了 service命令

参考:redhat文档:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Services.html#sect-Managing_Services_with_systemd-Services-List

查看全部服务命令:
systemctl list-unit-files --type service
查看服务
systemctl status name.service
启动服务
systemctl start name.service
停止服务
systemctl stop name.service
重启服务
systemctl restart name.service增加开机启动
systemctl enable name.service
删除开机启动
systemctl disable name.service
其中.service 可以省略。

添加环境变量:

## JAVA
export JAVA_HOME=/usr/local/java/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

## TOMCAT
export CATALINA_HOME=/usr/local/tomcat

## PATH
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:$JAVA_HOME/bin:$CATALINA_HOME/bin

catalina.sh加载环境变量

head ./bin/catalina.sh 

#!/bin/sh

. /etc/profile


# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0

2,tomcat增加启动参数

 把apache-tomcat 解压为 /usr/local/tomcat

tomcat 需要增加一个pid文件

在tomca/bin 目录下面,增加 setenv.sh 配置,添加可执行权限,catalina.sh启动的时候会调用,同时配置java内存参数,注意这里的pid文件路径要与systemctl脚本里的pid文件一致

#add tomcat pid
CATALINA_PID="$CATALINA_HOME/tomcat.pid"

#add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

3,增加tomcat.service

在/usr/lib/systemd/system目录下增加tomcat.service,目录必须是绝对目录。

 cat /usr/lib/systemd/system/tomcat.service


[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target


[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/catalina.sh start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true


[Install]
WantedBy=multi-user.target

 


[unit]配置了服务的描述,规定了在network启动之后执行。[service]配置服务的pid,服务的启动,停止,重启。[install]配置了使用用户。

4,使用tomcat.service

配置开机启动 

systemctl enable tomcat

启动tomcat
systemctl start tomcat
停止tomcat
systemctl stop tomcat
重启tomcat
systemctl restart tomcat

因为配置pid,在启动的时候会再tomcat根目录生成tomcat.pid文件,停止之后删除。

同时tomcat在启动时候,执行start不会启动两个tomcat,保证始终只有一个tomcat服务在运行。

多个tomcat可以配置在多个目录下,互不影响

# Systemd unit file for default tomcat
# 
# To create clones of this service:
# DO NOTHING, use tomcat@.service instead.

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=simple
EnvironmentFile=/etc/tomcat/tomcat.conf
Environment="NAME="
EnvironmentFile=-/etc/sysconfig/tomcat
ExecStart=/usr/libexec/tomcat/server start
SuccessExitStatus=143
User=tomcat
Group=tomcat


[Install]
WantedBy=multi-user.target
grep -v "^#" /etc/tomcat/tomcat.conf

TOMCAT_CFG_LOADED
="1" TOMCATS_BASE="/var/lib/tomcats/" JAVA_HOME="/usr/lib/jvm/jre" CATALINA_HOME="/usr/share/tomcat" CATALINA_TMPDIR="/var/cache/tomcat/temp" SECURITY_MANAGER="false"
cat /etc/sysconfig/tomcat
# Service-specific configuration file for tomcat. This will be sourced by
# the SysV init script after the global configuration file
# /etc/tomcat/tomcat.conf, thus allowing values to be overridden in
# a per-service manner.
#
# NEVER change the init script itself. To change values for all services make
# your changes in /etc/tomcat/tomcat.conf
#
# To change values for a specific service make your edits here.
# To create a new service create a link from /etc/init.d/<your new service> to
# /etc/init.d/tomcat (do not copy the init script) and make a copy of the
# /etc/sysconfig/tomcat file to /etc/sysconfig/<your new service> and change
# the property values so the two services won't conflict. Register the new
# service in the system as usual (see chkconfig and similars).
#

# Where your java installation lives
#JAVA_HOME="/usr/lib/jvm/java"

# Where your tomcat installation lives
#CATALINA_BASE="/usr/share/tomcat"
#CATALINA_HOME="/usr/share/tomcat"
#JASPER_HOME="/usr/share/tomcat"
#CATALINA_TMPDIR="/var/cache/tomcat/temp"

# You can pass some parameters to java here if you wish to
#JAVA_OPTS="-Xminf0.1 -Xmaxf0.3"

# Use JAVA_OPTS to set java.library.path for libtcnative.so
#JAVA_OPTS="-Djava.library.path=/usr/lib64"

# What user should run tomcat
#TOMCAT_USER="tomcat"

# You can change your tomcat locale here
#LANG="en_US"

# Run tomcat under the Java Security Manager
#SECURITY_MANAGER="false"

# Time to wait in seconds, before killing process
#SHUTDOWN_WAIT="30"

# Whether to annoy the user with "attempting to shut down" messages or not
#SHUTDOWN_VERBOSE="false"

# Connector port is 8080 for this tomcat instance
#CONNECTOR_PORT="8080"

# If you wish to further customize your tomcat environment,
# put your own definitions here
# (i.e. LD_LIBRARY_PATH for some jdbc drivers)
cat /usr/libexec/tomcat/server
#!/bin/bash

. /usr/libexec/tomcat/preamble

MAIN_CLASS=org.apache.catalina.startup.Bootstrap

FLAGS="$JAVA_OPTS"
OPTIONS="-Dcatalina.base=$CATALINA_BASE 
-Dcatalina.home=$CATALINA_HOME 
-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS 
-Djava.io.tmpdir=$CATALINA_TMPDIR 
-Djava.util.logging.config.file=${LOGGING_PROPERTIES} 
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

if [ "$1" = "start" ] ; then
  FLAGS="${FLAGS} $CATALINA_OPTS"
  if [ "${SECURITY_MANAGER}" = "true" ] ; then
    OPTIONS="${OPTIONS} 
    -Djava.security.manager 
    -Djava.security.policy==${CATALINA_BASE}/conf/catalina.policy"
  fi
  run start
elif [ "$1" = "stop" ] ; then
  run stop
fi
原文地址:https://www.cnblogs.com/linkenpark/p/7554772.html