centos7 使用systemctl设置tomcat9服务
CentOS7开始,从/etc/init.d脚本改为了systemctl管理服务。
本文记录了把tomcat9加入systemd管理的步骤。
1.首先,需要为tomcat配置pid。
# cd /usr/local/tomcat//usr/local/tomcat/apache-tomcat-9.0.21/bin
# ls
编辑 catalina.sh脚本文件
# vim catalina.sh
# Get standard environment variables
PRGDIR=`dirname "$PRG"`
# Only set CATALINA_HOME if not already set
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
# Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
#添加的pid
# setting tomcat.pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
2.创建tomcat.service文件
# cd /usr/lib/systemd/system
# vim tomcat.service
文件内容
##########################################################
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/local/java/jdk1.8.0_211"
Environment="CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.21"
PIDFile=/usr/local/tomcat/apache-tomcat-9.0.21/tomcat.pid
ExecStart=/usr/local/tomcat/apache-tomcat-9.0.21/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
##########################################################
3.修改tomcat.service文件后需要执行下面命令使其生效:
# systemctl daemon-reload
启动tomcat服务
systemctl start tomcat.service
设置开机自启动
systemctl enable tomcat.service
停止开机自启动
systemctl disable tomcat.service
查看服务当前状态
systemctl status tomcat.service
重新启动服务
systemctl restart tomcat.service
查看所有已启动的服务
systemctl list-units --type=service