第一步:修改/etc/rc.d/rc.local
![e03abbb8b0d94488ae96a2f1bd776b44.png](https://i-blog.csdnimg.cn/blog_migrate/8c291172891ef39702cae1ca855baa38.png)
第二步:在文件最后加上两行路径
export JAVA_HOME=/usr/local/java/jdk1.8.0_221
/usr/local/tomcat/apache-tomcat-7.0.90/bin/startup.sh start
# 别忘了修改上面的路径。
第三步:给rc.local文件添加执行权限
chmod +x /etc/rc.d/rc.local
第四步:重启验证
reboot
第二种方法,tomcat脚本自启。(ps:需要在root下执行)
1.创建并编辑tamcat
vim /etc/init.d/tomcat
2.把代码粘贴到tomcat里面
#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcat
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
export JRE_HOME=/usr/local/java/jdk1.8.0_221
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=/usr/local/tomcat
start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo " OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 1
ps -fwwu tomcat | grep apache-tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return $RETVAL
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring Tomcat"
$0 stop
sleep 1
$0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
3.设定开机启动服务
sudo chkconfig tomcat on
4.第四步、验证一下 reboot 重启