设置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 ~]# 
相关推荐
黑胡子大叔的小屋41 分钟前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark44 分钟前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding2 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
V+zmm101342 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm
文大。3 小时前
2024年广西职工职业技能大赛-Spring
java·spring·网络安全
一只小小翠3 小时前
EasyExcel 模板+公式填充
java·easyexcel
m0_748256343 小时前
QWebChannel实现与JS的交互
java·javascript·交互
Jelena技术达人4 小时前
Java爬虫获取1688关键字 item_search接口返回值详细解析
java·开发语言·爬虫
u0107735144 小时前
【字符串】-Lc5-最长回文子串(中心扩展法)
java·算法