vim /etc/profile
在最后一行添加
CATALINA_HOME=/usr/local/src/tomcat/tomcat8.5
输入下面命令让设置的环境变量生效
source /etc/profile
设置tomcat开机自启
cd /etc/init.d
vi tomcat
添加内容:
#cription: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 2345 20 80
#idea - tomcat config start
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 2345 20 80
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/usr/local/tomcat
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
#sh $CATALINA_HOME2/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
#sh $CATALINA_HOME2/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
#sh $CATALINA_HOME2/bin/shutdown.sh
#sh $CATALINA_HOME2/bin/startup.sh
;;
esac
exit 0
#chmod 755 tomcat
#chkconfig --add tomcat
#chkconfig --level 2345 tomcat on
设置权限及添加服务
为 tomcat 分配可执行权限:
chmod +x tomcat
#或者
chmod 755 tomcat
在/usr/lib/systemd/system目录下增加tomcat.service文件, 内容如下:
[Unit]
Description=Tomcat8
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment='JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64'
Environment='CATALINA_PID=/usr/local/tomcat/bin/tomcat.pid'
Environment='CATALINA_HOME=/usr/local/tomcat/'
Environment='CATALINA_BASE=/usr/local/tomcat/'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
WorkingDirectory=/usr/local/tomcat/
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
添加tomcat为系统服务:
chkconfig --add tomcat
验证服务
#启动
service tomcat start
#停止
service tomcat stop
#重启
service tomcat restart
添加到开机自启动
systemctl enable tomcat
最后执行命令查看是否添加成功
systemctl list-unit-files
tomcat.service enabled (enabled 表示开机自启设置成功)