设置tomcat应用systemd启动服务

目的:

一般二进制安装的tomcat应用是没有配置systemd启动服务的,可以按照需求自行配置使用systemd服务启停tomcat应用会更加的方便。

一、配置Tomcat应用service服务

bash 复制代码
# 复制粘贴以下内容完成配置tomcat的systemd启停服务
cat > /etc/systemd/system/tomcat.service  << EOF
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

# 设置 JAVA 环境变量,修改为你的 JAVA_HOME 目录
Environment=JAVA_HOME=/usr/local/jdk1.8.0_161

# 设置 tomcat 应用pid文件
# Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid

# tomcat 应用安装目录, 修改成你的 tomcat 安装目录
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat

# 设置 JVM 参数,不建议直接在这里设置,可以在 bin目录下的 setenv.sh 文件配置jvm等相关参数
# Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

# tomcat 启动命令,修改成你的启动命令的实际绝对路径
ExecStart=/opt/tomcat/bin/startup.sh
# tomcat 停止命令,修改成你的停止命令的实际绝对路径
ExecStop=/opt/tomcat/bin/shutdown.sh

# 运行用户,正常情况是使用非 root 用户,如 tomcat 用户
User=root
Group=root
UMask=0007

# 异常则自动尝试重启
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

EOF

# 重新加载 systemd
systemctl daemon-reload

# 启动 tomcat
systemctl start tomcat

# 停止 tomcat
systemctl stop tomcat

# 查看 tomcat 运行状态
systemctl status tomcat

二、演示例子

bash 复制代码
[root@lighthouse ~]# 
[root@lighthouse ~]# # 查看tomcat的service配置信息           
[root@lighthouse ~]# cat   /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

# 设置 JAVA 环境变量
Environment=JAVA_HOME=/usr/local/jdk1.8.0_161

# 设置 tomcat 应用pid文件
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid

# tomcat 应用安装目录
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat

# 设置 JVM 参数,不建议直接在这里设置,可以在 bin目录下的 setenv.sh 文件配置jvm等相关参数
# Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

# tomcat 启动命令
ExecStart=/opt/tomcat/bin/startup.sh
# tomcat 停止命令
ExecStop=/opt/tomcat/bin/shutdown.sh

# 运行用户,正常情况是使用非 root 用户,如 tomcat 用户
User=root
Group=root
UMask=0007

# 异常则自动尝试重启
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# # 查看 tomcat 运行状态                  
[root@lighthouse ~]# systemctl status tomcat                 
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-07-19 09:36:06 CST; 19min ago
 Main PID: 1524 (java)
   CGroup: /system.slice/tomcat.service
           └─1524 /usr/local/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager...

Jul 19 09:36:06 lighthouse systemd[1]: Starting Apache Tomcat Web Application Container...
Jul 19 09:36:06 lighthouse startup.sh[1515]: Tomcat started.
Jul 19 09:36:06 lighthouse systemd[1]: Started Apache Tomcat Web Application Container.
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# # 停止 tomcat 应用
[root@lighthouse ~]# systemctl stop  tomcat 
[root@lighthouse ~]# 
[root@lighthouse ~]# # 查看 tomcat 运行状态 
[root@lighthouse ~]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2023-07-19 09:56:33 CST; 41s ago
  Process: 7484 ExecStop=/opt/tomcat/bin/shutdown.sh (code=exited, status=1/FAILURE)
 Main PID: 1524 (code=exited, status=143)

Jul 19 09:36:06 lighthouse systemd[1]: Starting Apache Tomcat Web Application Container...
Jul 19 09:36:06 lighthouse startup.sh[1515]: Tomcat started.
Jul 19 09:36:06 lighthouse systemd[1]: Started Apache Tomcat Web Application Container.
Jul 19 09:56:33 lighthouse systemd[1]: Stopping Apache Tomcat Web Application Container...
Jul 19 09:56:33 lighthouse shutdown.sh[7484]: $CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.
Jul 19 09:56:33 lighthouse systemd[1]: tomcat.service: control process exited, code=exited status=1
Jul 19 09:56:33 lighthouse systemd[1]: tomcat.service: main process exited, code=exited, status=143/n/a
Jul 19 09:56:33 lighthouse systemd[1]: Stopped Apache Tomcat Web Application Container.
Jul 19 09:56:33 lighthouse systemd[1]: Unit tomcat.service entered failed state.
Jul 19 09:56:33 lighthouse systemd[1]: tomcat.service failed.
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# # 启动 tomcat 应用
[root@lighthouse ~]# systemctl start  tomcat
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# 
[root@lighthouse ~]# # 查看 tomcat 运行状态 
[root@lighthouse ~]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-07-19 09:57:42 CST; 18s ago
  Process: 7484 ExecStop=/opt/tomcat/bin/shutdown.sh (code=exited, status=1/FAILURE)
  Process: 7933 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 7940 (java)
    Tasks: 30
   Memory: 93.3M
   CGroup: /system.slice/tomcat.service
           └─7940 /usr/local/jdk1.8.0_161/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager...

Jul 19 09:57:42 lighthouse systemd[1]: Starting Apache Tomcat Web Application Container...
Jul 19 09:57:42 lighthouse startup.sh[7933]: Tomcat started.
Jul 19 09:57:42 lighthouse systemd[1]: Started Apache Tomcat Web Application Container.
[root@lighthouse ~]# 
[root@lighthouse ~]# 
相关推荐
不是二师兄的八戒15 分钟前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
爱编程的小生27 分钟前
Easyexcel(2-文件读取)
java·excel
带多刺的玫瑰44 分钟前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
计算机毕设指导61 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
Gu Gu Study1 小时前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言
Chris _data1 小时前
二叉树oj题解析
java·数据结构
牙牙7052 小时前
Centos7安装Jenkins脚本一键部署
java·servlet·jenkins
paopaokaka_luck2 小时前
[371]基于springboot的高校实习管理系统
java·spring boot·后端
以后不吃煲仔饭2 小时前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师2 小时前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言