如果你有一个tomcat的二进制包你就可以使用以下.service文件直接套用,前提是你必须先停止现有的tomcat
cat tomcat.service
[Unit]
Description=tomcat server daemon # 描述
After=syslog.target network.target remote-fs.target nss-lookup.target # 在那些服务之后启动
[Service]
Type=forking
Environment='JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.22.0.7-1.el7_9.x86_64'
Environment='CATALINA_PID=/var/run/tomcat.pid' # 文件需要创建
Environment='CATALINA_HOME=/root/apache-tomcat-9.0.88'
Environment='CATALINA_BASE=/root/apache-tomcat-9.0.88'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
WorkingDirectory=/root/apache-tomcat-9.0.88
PIDFile=/var/run/tomcat.pid
ExecStart=/root/apache-tomcat-9.0.88/bin/startup.sh
ExecReload=/root/apache-tomcat-9.0.88/bin/catalina.sh restart
ExecStop=/root/apache-tomcat-9.0.88/bin/catalina.sh stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 移动到系统读取.service 的目录
mv tomcat.service /usr/lib/systemd/system/
# 重新加载
systemctl daemon-reload
# 创建pid文件
touch /var/run/tomcat.pid
# 启动并自启
systemctl enable tomcat --now
参考: https://blog.csdn.net/wd520521/article/details/118212338
https://blog.csdn.net/m0_45406092/article/details/130588316