tomcat配置systemctl管理服务和shell脚本文件管理

确保有java环境和tomcat环境

1.配置service方式管理

(1)配置/etc/profile

路径使用你的jdk和tomcat的目录路径

(2)配置服务文件tomcat.service

根据自己的实际目录进行配置,JAVA_HOME=和CATALINA_HOME=两个的配置和上面/etc/profile保持一致

此处是配置的tomcat用户进行启动(可以更改为已有用户,需考虑权限问题),那么还需要将tomcat的相关所有文件属主和属组改为tomcat

 (3)重载配置,测试tomcat服务

2.通过shell脚本方式管理

(1) 编写shell脚本

(2)运行测试 


确保有java环境和tomcat环境

[root@localhost java]# java -version
java version "1.8.0_371"
Java(TM) SE Runtime Environment (build 1.8.0_371-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.371-b11, mixed mode)

[root@localhost ~]# /usr/local/tomcat/apache-tomcat-8.5.70/bin/version.sh 
Using CATALINA_BASE:   /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME:   /usr/local/tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_371
Using CLASSPATH:       /usr/local/tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Server version: Apache Tomcat/8.5.70
Server built:   Aug 9 2021 16:17:14 UTC
Server number:  8.5.70.0
OS Name:        Linux
OS Version:     3.10.0-1160.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_371-b11
JVM Vendor:     Oracle Corporation

1.配置service方式管理

(1)配置/etc/profile

路径使用你的jdk和tomcat的目录路径

[root@localhost ~]# tail -4 /etc/profile 
export JAVA_HOME=/usr/java/jdk1.8.0_371
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
export TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
[root@localhost ~]# source /etc/profile

(2)配置服务文件tomcat.service

根据自己的实际目录进行配置,JAVA_HOME=和CATALINA_HOME=两个的配置和上面/etc/profile保持一致

[root@localhost system]# pwd
/usr/lib/systemd/system
[root@localhost system]# vim tomcat.service 
# Systemd unit file for default tomcat
# 
# To create clones of this service:
# DO NOTHING, use [email protected] instead.

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

[Service]
Type=forking
Environment=JAVA_HOME=/usr/java/jdk1.8.0_371
Environment=CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.70/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-8.5.70/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
SuccessExitStatus=143
User=tomcat
Group=tomcat
PrivateTmp=true

[Install]
WantedBy=multi-user.target

此处是配置的tomcat用户进行启动(可以更改为已有用户,需考虑权限问题),那么还需要将tomcat的相关所有文件属主和属组改为tomcat

[root@localhost ~]# chown -R tomcat:tomcat /usr/local/tomcat/

 (3)重载配置,测试tomcat服务

[root@localhost system]# systemctl daemon-reload 
[root@localhost system]# systemctl stop tomcat
[root@localhost system]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Aug 06 22:24:30 localhost.localdomain systemd[1]: tomcat.servic...
Aug 06 22:24:42 localhost.localdomain systemd[1]: Starting Apac...
Aug 06 22:24:42 localhost.localdomain systemd[1]: Started Apach...
Aug 06 22:33:29 localhost.localdomain systemd[1]: Stopping Apac...
Aug 06 22:33:29 localhost.localdomain systemd[1]: Stopped Apach...
Aug 06 22:33:29 localhost.localdomain systemd[1]: Starting Apac...
Aug 06 22:33:29 localhost.localdomain startup.sh[33059]: Tomcat...
Aug 06 22:33:29 localhost.localdomain systemd[1]: Started Apach...
Aug 06 22:34:20 localhost.localdomain systemd[1]: Stopping Apac...
Aug 06 22:34:20 localhost.localdomain systemd[1]: Stopped Apach...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost system]# 
[root@localhost system]# systemctl start tomcat
[root@localhost system]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2023-08-06 22:34:50 CST; 1s ago
  Process: 33218 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.70/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 33225 (java)
   CGroup: /system.slice/tomcat.service
           └─33225 /usr/java/jdk1.8.0_371/bin/java -Djava.util....

Aug 06 22:34:50 localhost.localdomain systemd[1]: Starting Apac...
Aug 06 22:34:50 localhost.localdomain systemd[1]: Started Apach...
Hint: Some lines were ellipsized, use -l to show in full.

2.通过shell脚本方式管理

(1) 编写shell脚本

[root@localhost tomcat]# vim tomcatmg.sh
#!/bin/bash
tomcat_home=/tomcat/apache-tomcat-8.5.70
tomcatdown=$tomcat_home/bin/shutdown.sh
tomcatstart=$tomcat_home/bin/startup.sh
pidtomcat=`ps -ef | grep $tomcat_home | grep -v "grep" | awk '{print $2}'`
case $1 in
start)
$tomcatstart
echo "tomcat启动"
;;
stop)
$tomcatdown
echo "tomcat关闭"
kill -9 $pidtomcat
;;
restart)
$tomcatdown
kill -9 $pidtomcat
sleep 2
$tomcatstart
echo "tomcat重启"
;;
esac

(2)运行测试 

[root@localhost tomcat]# vim tomcatmg,sh 

[root@localhost tomcat]# bash tomcatmg.sh stop
Using CATALINA_BASE:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME:        /home/install/jdk/jdk1.8.0_371
Using CLASSPATH:       /tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
tomcat关闭
[root@localhost tomcat]# netstat -lntup | grep 8080

[root@localhost tomcat]# bash tomcatmg.sh start
Using CATALINA_BASE:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME:        /home/install/jdk/jdk1.8.0_371
Using CLASSPATH:       /tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
tomcat启动
[root@localhost tomcat]# netstat -lntup | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      8937/java           

[root@localhost tomcat]# bash tomcatmg.sh restart
Using CATALINA_BASE:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME:        /home/install/jdk/jdk1.8.0_371
Using CLASSPATH:       /tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Using CATALINA_BASE:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_HOME:   /tomcat/apache-tomcat-8.5.70
Using CATALINA_TMPDIR: /tomcat/apache-tomcat-8.5.70/temp
Using JRE_HOME:        /home/install/jdk/jdk1.8.0_371
Using CLASSPATH:       /tomcat/apache-tomcat-8.5.70/bin/bootstrap.jar:/tomcat/apache-tomcat-8.5.70/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
tomcat重启
[root@localhost tomcat]# netstat -lntup | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      9005/java           
[root@localhost tomcat]# 

你可能感兴趣的:(tomcat,Linux,tomcat,服务器,java)