CentOS7.6设置tomcat自启
vi /usr/lib/systemd/system/tomcat8.service
文件内容如下
[Unit]
Description=Tomcat8
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment='JAVA_HOME=/usr/java'
Environment='CATALINA_PID=/usr/local/tomcat8/bin/tomcat.pid'
Environment='CATALINA_HOME=/usr/local/tomcat8/'
Environment='CATALINA_BASE=/usr/local/tomcat8/'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
WorkingDirectory=/usr/local/tomcat8/
ExecStart=/usr/local/tomcat8/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
退出并保存文件
注意:需要注意的文件夹名字和路径一定要对应
执行systemctl enable tomcat8.service使tomcat开机启动
systemctl start tomcat8.service 启动tomcat
systemctl stop tomcat8.service 结束tomcat
systemctl restart tomcat8.service 重启tomcat
systemctl status tomcat8.service 查看tomcat状态
如果要设置多个TOMCAT启动,可以修改服务名字和路径,举例如下
vi /usr/lib/systemd/system/tomcat8081.service
[Unit]
Description=Tomcat8081
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment='JAVA_HOME=/usr/java'
Environment='CATALINA_PID=/usr/local/tomcat8081/bin/tomcat.pid'
Environment='CATALINA_HOME=/usr/local/tomcat8081/'
Environment='CATALINA_BASE=/usr/local/tomcat8081/'
WorkingDirectory=/usr/local/tomcat8081/
ExecStart=/usr/local/tomcat8081/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
退出保存
另外要注意的是一旦修改自启服务配置文件,就要让 SystemD 重新加载配置文件,然后重新启动,否则修改不会生效。命令为systemctl daemon-reload
vim /usr/lib/systemd/system/tomcat9.service
[Unit]
Description=Tomcat9
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment='JAVA_HOME=/usr/java'
Environment='CATALINA_PID=/usr/local/tomcat9/bin/tomcat.pid'
Environment='CATALINA_HOME=/usr/local/tomcat9/'
Environment='CATALINA_BASE=/usr/local/tomcat9/'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
WorkingDirectory=/usr/local/tomcat9/
ExecStart=/usr/local/tomcat9/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target