CentOS7下配置tomcat开机自启

时间:2021-02-27 19:15:37

1.创建tomcat自动启动命令脚本

vi /etc/init.d/tomcat

2.写以下代码:

注意修改JAVA_HOME和CATALINA_HOME CATALINA_BASE字段, 匹配自己的安装路径 。

#!/bin/sh 
# chkconfig: 345 99 10 
# description: Auto-starts tomcat 
# /etc/init.d/tomcatd 
# Tomcat auto-start 
# Source function library. 
#. /etc/init.d/functions 
# source networking configuration. 
#. /etc/sysconfig/network 
RETVAL=0  
export JAVA_HOME=/usr/java/jdk1.8
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 root | grep 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.设置执行权限
chmod a+x /etc/init.d/tomcat

4.注册成服务
chkconfig –add tomcat

5.设置开机启动
chkconfig tomcaton