环境:

Centos7、JDK 1.8、Tomcat9

安装好JDK跟Tomcat后在/usr/lib/systemd/system/目录下新建文件tomcat.service,内容如下,对应的位置替换一下

[Unit]
Description=Tomcat9
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
Environment='JAVA_HOME=/opt/jdk/jdk' #JDK安装位置
Environment='CATALINA_PID=/opt/tomcat9/bin/tomcat.pid'
Environment='CATALINA_HOME=/opt/tomcat9/'#Tomcat安装位置
Environment='CATALINA_BASE=/opt/tomcat9/'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

WorkingDirectory=/opt/tomcat9/

ExecStart=/opt/tomcat9/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置为开启机启动:systemctl enable tomcat

其它命令:

启动服务:systemctl start tomcat

停止服务:systemctl stop tomcat

重启服务:systemctl restart tomcat

检查状态:systemctl status tomcat

重启机子后可以看下是否Tomcat已经随机启动了:http://192.168.95.198:8080/