设置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 ~]# 
相关推荐
WaaTong11 分钟前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
m0_7430484411 分钟前
初识Java EE和Spring Boot
java·java-ee
AskHarries13 分钟前
Java字节码增强库ByteBuddy
java·后端
小灰灰__33 分钟前
IDEA加载通义灵码插件及使用指南
java·ide·intellij-idea
夜雨翦春韭36 分钟前
Java中的动态代理
java·开发语言·aop·动态代理
程序媛小果1 小时前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot
追风林1 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
芒果披萨1 小时前
El表达式和JSTL
java·el
duration~2 小时前
Maven随笔
java·maven
zmgst2 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql